In a belongs_to association, is there a way to tell if the associated object was newly created? Hopefully this will explain my question: A Firm class declares has_many<http://api.rubyonrails.com/classes/ActiveRecord/As... and a client class declares belongs_to<http://api.rubyonrails.com/classes/ActiveRecord/As.... Now you do the following: Create a new firm and add clients using firm.clients.create methods. Note both firm and clients are unsaved at this point. Then you save the firm. It appears the sequence of events is 1) save the firm 2) save the clients Is it possible for the clients to know if the firm was newly saved? By the time the client is being saved firm.new_record? is false. Any suggestions? Thanks, -Kelly
on 2006-06-03 23:46
on 2006-06-04 03:47
Kelly Felkins wrote: > > Is it possible for the clients to know if the firm was newly saved? By > the time the client is being saved firm.new_record? is false. Firm instances will have an appropriately set boolean instance variable called @new_record_before_save, which the clients will be able to inspect if you add a reader method for it. But because this is undocumented, and so subject to change, it'd be better if you made your own by adding to the firm model something like: before_save("@newly_saved? = new_record?; true") after_save("@newly_saved? = false; true") # optional attr_reader :newly_saved? -- We develop, watch us RoR, in numbers too big to ignore.