问题描述
考虑A类的基本结构有很多B。 现在,在克隆对象时,我想跳过对象B的回调。该怎么做? 我们通常使用attr_accessor来执行此操作,但我也无法做到这一点。
https://github.com/amoeba-rb/amoeba/issues/17
此问题由来已久。
class File < ApplicationRecord
amoeba do
enable
include_association :attachments
end
has_many :attachments
end
class Attachment < ApplicationRecord
attr_accessor :skip_processing
amoeba do
enable
# This is wrong
set :skip_processing => true
end
belongs_to :file
after_commit :process_attachment,on: :create,unless: :skip_processing
end
在变形虫块中使用attr_accessor时出现一些错误,我认为我们只能使用DB值。 有什么解决办法吗?
解决方法
Amoeba gem提供了不同的预处理器,其中之一是我在这里使用的自定义。 您可以传递lambda函数或lambda函数数组,在其中可以调用方法或为克隆对象设置属性。我用它来设置attr_accessor如下-
class Attachment < ApplicationRecord
attr_accessor :skip_processing
amoeba do
enable
customize (lambda { |original,cloned|
# Set attr_accessor here
cloned.skip_processing = true
})
end
belongs_to :file
after_commit :process_attachment,on: :create,unless: :skip_processing
end