【问题标题】:Callback after_destroy not triggered through ActiveAdmin回调 after_destroy 未通过 ActiveAdmin 触发
【发布时间】:2018-10-10 06:49:53
【问题描述】:

我在我的应用程序的后台使用 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

在我的Organizationf.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

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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 callback activeadmin


    【解决方案1】:

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

      after_destroy do |organization|
        # do your stuff
      end
    

    我之前为 before_save 和 after_save 做过我不确定它是否可用于 after_destroy 你可以查看here 更多关于活动管理员回调的信息。

    【讨论】:

    • 谢谢!我将尝试使用after_destroy,但使用OrganizationCollectionRelation 模型,这对我来说很重要。
    • 回调对我不起作用。 OrganizationCollectionRelation 模型的after_destroy 回调不是由Organization 的编辑触发的
    • 您在活动管理员中是否有 organization_collection_relations.rb 文件??
    • 是的!我实际上在该文件中添加了after_detroy
    • 能否请您添加您添加的代码以及您在 after_destroy 中遇到的错误?
    【解决方案2】:

    是的,你可以试试 admin 文件中的after_destory 方法,比如

     after_destroy do |organization|
        organization.organization_collection_relations.each(&:do_something)
      end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-12
      • 2020-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多