我使用 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)
我认为有更好的方法来处理这个问题......