Rails: How to perform callbacks on a model that is not updated but is related to updated model
to KIS I have two models: Reservations
and Containers
.
Container
h开发者_如何转开发as_many :reservations
Reservation
belongs_to :container
When I update a reservation I also want to perform call backs on the respective Container.
What is a good way to do this?
Should I be using nested rest routes, put the container logic in the Reservation model, or something else
Rails has an option called touch
.
class Reservation < ActiveRecord::Base
belongs_to :container, :touch => true
end
Rails updates the updated_at
field of the Container
object when ever Reservation
object changes. If you have any callbacks in Container
class they will be invoked when Reservation
object changes.
class Container < ActiveRecord::Base
has_many :reservations
after_update :reservation_change
def reservation_change
# handle the updates..
end
end
精彩评论