开发者

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
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜