Papertrail轨道可以创建但不能通过关联删除

时间:2018-10-10 07:59:01

标签: activerecord rails-activerecord paper-trail-gem

我有一个行业表,并且正在跟踪也是行业的竞争对手。这是通过具有Industry_id和competitor_id的映射表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 FROM `industry_competitors` WHERE `industry_competitors`.`industry_id` = 4559 AND `industry_competitors`.`competitor_id` = 4564

我怀疑是因为activerecord调用'delete'而不是'destroy',所以不会触发该纸张提示回调,因此不会跟踪更改。 如果有调用的方法,则显式删除(只需最少的代码更改)。还是papertrail有办法跟踪删除?

1 个答案:

答案 0 :(得分:0)

添加此补丁可以使其正常工作。

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