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.

if grading.question_id is nil then grading.question will also be nil.
So you're effectively doing the same check.

I hope this helps

RobL

On 13 March 2010 15:54, Max Williams <li...@ruby-forum.com> wrote:
> Hi. I'm using rails 2.3.4 and have this association between two of my
> models.
>
> Question
>  has_many :gradings
>
> Grading
>  belongs_to :question
>
> Grading has this validation:
>  validates_presence_of :question_id
>
> And this validation test is stopping me from doingg this, which i would
> normally expect to work
>
> @question = Question.new(attributes)
> @grading = @question.gradings.build(more_attributes)
> @question.save #fails because grading has no question_id
>
> Now, i thought that this would work ok because (in my understanding) the
> question is saved and then it saves it's built associated objects.  How
> can i ensure this will work?  Do i need to drop the validation?  I'd
> rather not if possible.
>
> Grateful for any advice - max
> --
> 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-t...@googlegroups.com.
> To unsubscribe from this group, send email to 
> rubyonrails-talk+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>



-- 
Rob Lacey
cont...@robl.me
http://www.robl.me

-- 
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-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to