回调 after_destroy 未通过 ActiveAdmin 触发

cmu*_*gar 6 ruby-on-rails callback activeadmin ruby-on-rails-4

我使用 ActiveAdmin 作为我的应用程序的后台,我有以下三个模型:

class Organization
  has_many :organization_collection_relations
  has_many :collections, through: :organization_collection_relations
end

class OrganizationCollectionRelation
  belongs_to :organization
  belongs_to :collection

  after_destroy :do_something
end

class Collection
  has_many :organization_collection_relations
  has_many :organizations, through: :organization_collection_relations
end
Run Code Online (Sandbox Code Playgroud)

在我的编辑页面中,Organization我有 和f.input :collections。当我编辑和组织时,例如删除所有集合,问题就出现了。回调after_destroy方法do_something没有被触发。因此,我必须在活动管理文件的控制器部分中采取解决方法。

controller do
  def update
    resource = Organization.find(params[:id])
    former_ids = resource.collection_ids
    super
    new_ids = resource.reload.collection_ids
    # my logic here
  end
end
Run Code Online (Sandbox Code Playgroud)

我认为有更好的方法来处理这个问题......

Vis*_*hal 1

Active Admin 也有自己的回调,因此您可以在管理文件夹中的organizations.rb 文件中使用如下所示的回调。

  after_destroy do |organization|
    # do your stuff
  end
Run Code Online (Sandbox Code Playgroud)

我之前为 before_save 和 after_save 完成了它,我不确定它是否可用于 after_destroy ,您可以在此处查看有关活动管理回调的更多信息。