What''s the proper way to update a models timestamp when you add an object to one of it''s associations? For example, if I have a model, Business, that has_many :addresses, when I add a new Address, I''d like the updated_at timestamp for the business object to get updated. Doing: @business.addresses.create(....) Does not update the updated_at timestamp for @business. What''s the best way to get it to update? Thank you, Kenny --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Erol Fornoles
2008-Sep-19 12:57 UTC
Re: Updating timestamp when adding model via association
On Sep 19, 5:11 pm, Kenny <kennycarruth...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> What''s the proper way to update a models timestamp when you add an > object to one of it''s associations? > > For example, if I have a model, Business, that has_many :addresses, > when I add a new Address, I''d like the updated_at timestamp for the > business object to get updated. > > Doing: > > @business.addresses.create(....) > > Does not update the updated_at timestamp for @business. What''s the > best way to get it to update? > > Thank you, > KennyIf you''re using Rails 2.1 and have ActiveRecord::Base.partial_updates = true, you can try this: class Address belongs_to :business before_save :update_business private def update_business if self.business && self.business_id_changed? self.business.updated_at = Time.now end end --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org 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 -~----------~----~----~----~------~----~------~--~---