Saving a related object

Hello,

I have two models. A and B. A belongs_to B (and B has_one A). I have one
form
for creating both an A and a B, so I do:

a = A.new(params[:a])
a.b = B.new(params[:b])
if a.save…

and it works as intended, both objects get saved, or none, validation
happens.
But then I have the page to edit these objects. So I do:

a = A.find(a_id)
…latter on…
a.attributes = params[:a]
a.b.attributes = params[:b]
if a.save…

and there it is where it breaks. b seems to be never saved. I’m not sure
if it
is validated or not. What should I do to cascade the saves? or how
should I
save these two objects?

Thank you.

J. Pablo Fernández [email protected] (http://pupeno.com)

Did you look at

after_save ?

Maybe you can perform something inside it in the A model

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs