使用连接模型,在此示例设置中使用 has_many :through
class Collage
has_many :arrangements
has_many :photos, through: :arrangements
class Photo
has_many :arragements
has_many :collages, through: :arragements
end
class Arragement
belongs_to :photo
belongs_to _collage
end
照片可能会改变尺寸,这会导致拼贴改变
使用 touch: true
不会以这种方式工作,因为链不是“单向向上”,因为 arragement
指向 Photo
和 拼贴画
我怎样才能使照片更改(即触摸)也能触摸到它的拼贴画?
最佳答案
这是一个更短的版本:
class Photo
has_many :arragements
has_many :collages, through: :arragements
after_save { collages.find_each(&:touch) }
end
关于ruby-on-rails-4 - 带有 has_many :through associations 的轨道触摸链,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29215109/