Setting values in nested objects

Hi,

I have a model with nested objects similar to this:

class Person < ActiveRecord   has_many :addresses   accepts_nested_attributes_for :addresses   accepts_nested_attributes_for :home end class Address < ActiveRecord   belongs_to :person   has_one :home end class Home < ActiveRecord   belongs_to :person   belongs_to :address end

In my form, I'd like to offer the user the option of selecting the address and home from a selection or add a new home/address in a text field. If a home/address is selected in the selection, I want to ignore the text field. If the renamed blank field is selected, then grab the text field. If the value in the text field is already used, then reuse the ID. I'd also like to propagate the "address" and "person" values to home, but it seems I can only do one or the other with build.

I've been able to get all the form magic to work, but saving doesn't go as planned. I have to intercept the save, but I'm not sure where the best place is. Do I override "new" and "update" or do a "before_save" or ... ?

Thanks in advance, Mike

Just noticed I had a typo. Forgot to include "has_many :homes" in Person model. Fixed below.

Any advice on where to update the nested objects?

Thanks in advance, Mike

I think this great screencast should do help you :