Hi Max,
Have you tried:
@question = Question.create(attributes)
@grading = @question.gradings.build(more_attrs)
Using create instead of new will save the Object to the DB first, and
hence:
@question[:id] will/should/may not be nil when building your gradings.
I'm six months in to Rails, don't use build, and haven't needed to use
any validation yet - am currently extending the generator for my
current and future Apps, so I have held fire on my first app until I
get this completed, but if your only issue is that the FK question_id
requires @question[:id] to exist, then unless I'm mistaken(which I
could be), using create over new should work.......
HTH
Paul
On Mar 13, 3:54 pm, 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 viahttp://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