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?
Does not update the updated_at timestamp for @business. What’s the
best way to get it to update?
Thank you,
Kenny
If 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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.