Hi,
I have a model, Foo, that has_many Bars. It has a virtual attribute, current_baz, that returns the baz attribute of the latest Bar from the association (if one exists). I'd like to define Foo.current_baz= so that one can say my_foo.update_attributes(:current_baz => 7), and in particular, I'd like it to work when a new Foo is created. That way I can say
Foo.new(:current_baz => 7, ...)
and it should do
self.bars.build(:baz => 7)
Of course, it doesn't work, because the new Foo doesn't have an id yet and thus the new Bar isn't valid. I know how to work around this in a controller, creating first the Foo and then the Bar and wrapping both in a transaction. But I'd really like to keep this logic in the model if possible. It seems almost possible. Does anyone have any suggestions?