0

考虑一个A类的基本结构有很多B。现在在克隆A对象时,我想跳过对象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 值。有什么解决办法吗?

4

1 回答 1

1

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
于 2020-09-15T00:57:49.677 回答