使用联接模型,并在此示例设置上使用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和Collage
我怎么能这样做,这样一张照片的变化(即触摸)也会触摸它的拼贴?
发布于 2017-03-11 22:17:53
这里有一个更短的版本:
class Photo
has_many :arragements
has_many :collages, through: :arragements
after_save { collages.find_each(&:touch) }
end发布于 2015-03-23 17:18:18
你只需手动触摸它们。
class Photo
has_many :arragements
has_many :collages, through: :arragements
after_save do
collages.update_all updated_at: Time.now
end
endhttps://stackoverflow.com/questions/29215109
复制相似问题