Papertrail 轨道通过关联创建但不删除
Papertrail tracks create but not delete through association
我有一个 table 行业,并且正在跟踪它的竞争对手,这些竞争对手也是行业。这是通过具有 industry_id 和 competitor_id 的映射 table industry_competitors 实现的。我希望 papertrail 跟踪行业竞争对手的关联和分离。
class Industry < ApplicationRecord
has_many :industry_competitors, dependent: :destroy
has_many :competitors, through: :industry_competitors
end
class IndustryCompetitor < ApplicationRecord
has_paper_trail
belongs_to :industry
belongs_to :competitor, class_name: "Industry"
end
我的控制器代码就是这样。
competitors = ::Industry.where(id: params[:competitor_ids])
@industry.competitors = competitors
@industry.save
每次通过整个参赛者名单。如果我试图从行业中分离一些竞争对手(通过不将 ID 传递给控制器),则会触发 'Delete' 查询。
DELETE FROM `industry_competitors` WHERE `industry_competitors`.`industry_id` = 4559 AND `industry_competitors`.`competitor_id` = 4564
我怀疑是因为 activerecord 调用了 'delete' 而不是 'destroy' 没有触发此 papertrail 回调,因此没有跟踪更改。
如果有一种方法可以显式调用 delete(代码更改最少)。或者papertrail有没有办法跟踪删除?
添加这个补丁可以让它工作。
module HasManyThroughAssociationPatch
def delete_records(records, method)
method ||= :destroy
super
end
end
ActiveRecord::Associations::HasManyThroughAssociation.prepend(HasManyThroughAssociationPatch)
致谢:https://github.com/westonganger/paper_trail-association_tracking
我有一个 table 行业,并且正在跟踪它的竞争对手,这些竞争对手也是行业。这是通过具有 industry_id 和 competitor_id 的映射 table industry_competitors 实现的。我希望 papertrail 跟踪行业竞争对手的关联和分离。
class Industry < ApplicationRecord
has_many :industry_competitors, dependent: :destroy
has_many :competitors, through: :industry_competitors
end
class IndustryCompetitor < ApplicationRecord
has_paper_trail
belongs_to :industry
belongs_to :competitor, class_name: "Industry"
end
我的控制器代码就是这样。
competitors = ::Industry.where(id: params[:competitor_ids])
@industry.competitors = competitors
@industry.save
每次通过整个参赛者名单。如果我试图从行业中分离一些竞争对手(通过不将 ID 传递给控制器),则会触发 'Delete' 查询。
DELETE FROM `industry_competitors` WHERE `industry_competitors`.`industry_id` = 4559 AND `industry_competitors`.`competitor_id` = 4564
我怀疑是因为 activerecord 调用了 'delete' 而不是 'destroy' 没有触发此 papertrail 回调,因此没有跟踪更改。 如果有一种方法可以显式调用 delete(代码更改最少)。或者papertrail有没有办法跟踪删除?
添加这个补丁可以让它工作。
module HasManyThroughAssociationPatch
def delete_records(records, method)
method ||= :destroy
super
end
end
ActiveRecord::Associations::HasManyThroughAssociation.prepend(HasManyThroughAssociationPatch)
致谢:https://github.com/westonganger/paper_trail-association_tracking