开发者

Rails: How to perform callbacks on a model that is not updated but is related to updated model

开发者 https://www.devze.com 2022-12-28 08:23 出处:网络
to KIS I have two models: Reservations and Containers. Container h开发者_如何转开发as_many :reservations

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
0

精彩评论

暂无评论...
验证码 换一张
取 消