Ruby on Rails Thursday, December 30, 2010

I make two class job and step, its associate with
accepts_nested_attributes_for, has_many and belongs_to.
liks:

class Job < ActiveRecord::Base
has_many :steps, :dependent => :destroy
accepts_nested_attributes_for :steps

validate :job_validate , :on => :create
def job_validate
p "job validate"
end
end


class Step < ActiveRecord::Base
belongs_to :job

# here just test if called this validates_presence_of
validates_presence_of :name , :if => Proc.new { |step| p " step name
validates_presence_of" }
validate_on_create :step_validate

private
def step_validate
p self
p "step validate"
end
end


Tow migtations links:
class CreateJobs < ActiveRecord::Migration
def self.up
create_table :jobs do |t|
t.string :name

t.timestamps
end
end

def self.down
drop_table :jobs
end
end


class CreateSteps < ActiveRecord::Migration
def self.up
create_table :steps do |t|
t.string :name
t.references :job
t.timestamps
end
end

def self.down
drop_table :steps
end
end

My problem is that when i testing them in script/console as follow:
shell:$ script/console
Loading shelllocal environment (Rails 2.3.5)
>> job = Job.new
=> #<Job id: nil, name: nil, created_at: nil, updated_at: nil>
>> job.steps.build
=> #<Step id: nil, name: nil, job_id: nil, created_at: nil,
updated_at: nil>
>> job.save
" step name validates_presence_of"
#<Step id: nil, name: nil, job_id: nil, created_at: nil, updated_at:
nil>
"step validate"
" step name validates_presence_of"
#<Step id: nil, name: nil, job_id: nil, created_at: nil, updated_at:
nil>
"step validate"
"job validate"
=> true
>>


As you see the "step validate" called two times.I think "step
validate" be called once is correct. I don't know why its called tow
times? Who can help me?

thank you !

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment