Ruby on Rails Tuesday, July 31, 2012

In a popular Rails book:

http://books.google.com/books?id=slwLAqkT_Y0C&pg=PT366&lpg=PT366&dq=%22validates_associated+in+conjunction+with+validates_presence_of%22&source=bl&ots=9bZwHNjzvG&sig=aTN0WnknjzZ0WfRd9PeJGxQSAEU&hl=en&sa=X&ei=RagYUM_zH4T89gTW_YDwCw&ved=0CC8Q6AEwAA#v=onepage&q=%22validates_associated%20in%20conjunction%20with%20validates_presence_of%22&f=false

it states that if want to make sure that the association is valid on a
belongs_to, that is, make sure the parent is valid, then you use
validates_associated in conjunction with validates_presence_of:

class Project < ActiveRecord::Base
has_many :tasks
accepts_nested_attributes_for :tasks
end

class Task < ActiveRecord::Base
belongs_to :project

validates_presence_of :project_id
validates_associated :project
end


Unfortunately, this raises an issue because if the project validation
fails, then it won't have an id, and then when Rails goes to validate
the tasks, the task in turn will fail validation because they won't
have a project_id. So why would the book suggest this? Or is it the
accepts_nested_attributes_for that is causing the above behavior?

thanks for response

--
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 https://groups.google.com/groups/opt_out.

No comments:

Post a Comment