检查回形针附件是否已更改
Check if paperclip attachment has changed
因此,为了减少重复,我将图片附件从 Product 模型拆分到 Image 模型上,以便类似的产品可以共享一张图片。像这样
class Product < ActiveRecord::Base
has_many :imaged_items
has_many :images, :through => :imaged_items
end
class ImagedItems < ActiveRecord::Base
belongs_to :product
belongs_to :image
end
class Image < ActiveRecord::Base
has_many :imaged_items
has_many :products, :through => :imaged_items
end
我想做的是,如果我正在编辑产品,我想在保存之前检查图像是否发生了变化。如果图像已更改,我不想更新现有记录,我想创建一个新记录,因为旧图像可以被多个产品使用。
有什么办法吗?我被难住了。
通过 Class: ActiveRecord::Dirty ...You can use changed
person.changed # => []
person.name = 'bob'
person.changed # => ['name']
####OR ..other way as well
person.name = 'Bill'
person.name_changed? # => false
person.name_change # => nil
我在 after_update
回调中遇到问题,我使用 .dirty?
而不是 thumb_changed?
after_update :foo, if: Proc.new{
|model| model.title_changed? || model.summary_changed? || model.thumb.dirty?
}
非常适合我。希望对你有帮助
因此,为了减少重复,我将图片附件从 Product 模型拆分到 Image 模型上,以便类似的产品可以共享一张图片。像这样
class Product < ActiveRecord::Base
has_many :imaged_items
has_many :images, :through => :imaged_items
end
class ImagedItems < ActiveRecord::Base
belongs_to :product
belongs_to :image
end
class Image < ActiveRecord::Base
has_many :imaged_items
has_many :products, :through => :imaged_items
end
我想做的是,如果我正在编辑产品,我想在保存之前检查图像是否发生了变化。如果图像已更改,我不想更新现有记录,我想创建一个新记录,因为旧图像可以被多个产品使用。
有什么办法吗?我被难住了。
通过 Class: ActiveRecord::Dirty ...You can use changed
person.changed # => []
person.name = 'bob'
person.changed # => ['name']
####OR ..other way as well
person.name = 'Bill'
person.name_changed? # => false
person.name_change # => nil
我在 after_update
回调中遇到问题,我使用 .dirty?
而不是 thumb_changed?
after_update :foo, if: Proc.new{
|model| model.title_changed? || model.summary_changed? || model.thumb.dirty?
}
非常适合我。希望对你有帮助