简体   繁体   English

使用 update_column 方法更新 ActiveRecord model 时,如何跳过除纸迹之外的所有回调?

[英]How to skip all callbacks except paper-trail when updating an ActiveRecord model using update_column method?

I am new to rails.我是新来的轨道。 I want to track the versions of my record using paper_trail gem but I am not able to do so when I use the update_columns method.我想使用 paper_trail gem 跟踪我的记录版本,但是当我使用 update_columns 方法时我无法这样做。 I think overriding the update_columns method may help me but I don't know how exactly should I write the code?我认为覆盖 update_columns 方法可能对我有帮助,但我不知道我应该如何编写代码?

How to skip all callbacks except paper-trail when updating an ActiveRecord model using update_column method?使用 update_column 方法更新 ActiveRecord model 时,如何跳过除纸迹之外的所有回调? .. I use the update_columns method. .. 我使用 update_columns 方法。

Per the Rails docs , the update_columns method skips callbacks.根据Rails 文档update_columns方法会跳过回调。 This includes the callbacks installed by PT.这包括 PT 安装的回调。

I am new to rails.我是新来的轨道。 ... I think overriding the update_columns method may help me..? ...我认为覆盖 update_columns 方法可能对我有帮助..?

If you're new to rails, you should not override core rails methods.如果您不熟悉 rails,则不应覆盖核心 rails 方法。

Solutions解决方案

In descending order of preference:按偏好降序排列:

  1. Normally, I'd recommend using update instead of update_columns .通常,我建议使用update而不是update_columns But, you want to skip a proper subset of callbacks.但是,您想跳过适当的回调子集
  2. You can touch the record, and PT will record a Version in which only updated_at changed.您可以touch记录,PT 将记录一个只有updated_at发生变化的Version If you only need object and not object_changes , this is sufficient.如果您只需要object而不需要object_changes ,这就足够了。
  3. You can use PaperTrail::Version.create just like any other model.您可以像使用任何其他 model 一样使用PaperTrail::Version.create This is public API, but you're responsible for passing the correct attributes.这是公共 API,但您有责任传递正确的属性。
  4. You can use PaperTrail::RecordTrail#record_update .您可以使用PaperTrail::RecordTrail#record_update This is private API and is subject to change at any time without warning.这是私有的 API,可能随时更改,恕不另行通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM