Ruby on Rails Tuesday, July 31, 2012

I need to rephrase this.

Here's the code:


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

Project.create!(
:name => 'Something',
:task_attributes => [ { :name => '123' }, { :name => '456' } ]
)

The pattern of this is:
* Validate Project
* Validate Tasks
* Save Project
* Save Tasks

The book says:
"if you want to make sure that the association is valid on a
belongs_to, you have to use validates_associated in conjunction with
validates_presence_of". And that is what the above code does.

But the problem with the above technique is that when we call create!
on the project, it validates the project (and between :validate =>
true default on project an the validates_associated call on the
belongs_to, these two calls prompt to check if the association are
valid next), and since the project has not yet been saved, this will
raise an error, because project_id does not exist yet, since the
project has not been saved yet. So why does book say to use "
validates_presence_of :project_id"?


On Jul 31, 11:57 pm, John Merlino <stoici...@aol.com> wrote:
> In a popular Rails book:
>
> http://books.google.com/books?id=slwLAqkT_Y0C&pg=PT366&lpg=PT366&dq=%...
>
> 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