rubyonrailsin

A Ruby and Rails talk

Sunday, March 14, 2010


[Rails] Re: validates_presence_of :parent_id stopping save

by rubyonrailsin 0 comments

Tag


Share this post:
Design Float
StumbleUpon
Reddit

Rob Lacey wrote:
> I'd say the best way to deal with this is never validate for a
> association_id
>
> #validates_presence_of :question_id
> validates_presence_of :question
>
> would be better.

Hi Rob!

I played with this already, and got a surprising result:

>>@question = Question.new(attributes)
=><a question>
>>@grading = @question.gradings.build(other_attributes)
=><a grading>
>>@grading.question
=>nil

This surprised me because i thought that the use of build set up the
association both ways, ie in both objects, so that even though @grading
had no question_id it 'knew' that it was associated with @question. I
must admit that this aspect of rails (ie associations that have been
built but not saved yet) is something that i've always found a bit
magical and hence unpredicatable.
--
Posted via http://www.ruby-forum.com/.

--
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

Subscribe feeds via e-mail

Blog Archive