Ruby-on-rails 如何跳过 ActiveRecord 回调?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1342761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:40:53  来源:igfitidea点击:

How to skip ActiveRecord callbacks?

ruby-on-railsrails-activerecord

提问by Jakub Arnold

Possible Duplicate:
How can I avoid running ActiveRecord callbacks?

可能的重复:
如何避免运行 ActiveRecord 回调?

I have model like this

我有这样的模型

class Vote < ActiveRecord::Base  
    after_save :add_points_to_user

    .....
end

Is it possible to somehow force model to skip calling add_points_to_userwhen saved? Possibly something like ActiveRecord#deletevs ActiveRecord#destroy?

是否有可能以某种方式强制模型add_points_to_user在保存时跳过调用?可能类似于ActiveRecord#deletevs ActiveRecord#destroy

采纳答案by Stan Bright

For Rails 2, but not Rails 3 you can use these:

对于 Rails 2,但不是 Rails 3,您可以使用这些:

object.send(:create_without_callbacks)
object.send(:update_without_callbacks)

回答by tardate

For Rails 3, ActiveSupport::Callbacksgives you the necessary control. I was just facing the same challenge in a data integration scenario where normally-desirable-callbacks needed to be brushed aside. You can reset_callbacks en-masse, or use skip_callback to disable judiciously, like this:

对于 Rails 3,ActiveSupport::Callbacks为您提供了必要的控制。我只是在数据集成场景中面临同样的挑战,通常需要的回调需要被搁置一旁。您可以整体重置回调,或使用 skip_callback 明智地禁用,如下所示:

Vote.skip_callback(:save, :after, :add_points_to_user)

..after which you can operate on Vote instances with :add_points_to_user inhibited

..之后,您可以在禁止 :add_points_to_user 的情况下对 Vote 实例进行操作

回答by sheldonh

The following applies to rails 2, rails 3 and rails 4:

以下内容适用于导轨 2、导轨 3 和导轨 4:

http://guides.rubyonrails.org/v3.2.13/active_record_validations_callbacks.html#skipping-callbacks

http://guides.rubyonrails.org/v3.2.13/active_record_validations_callbacks.html#skiping-callbacks

It provides a list of methods that skip callbacks, explaining why it is dangerous to use them without careful consideration. Reprinted here under the provisions of the Creative Commons Attribution-Share Alike 3.0 License.

它提供了一个跳过回调的方法列表,解释了为什么不仔细考虑就使用它们是危险的。根据知识共享署名-相同方式共享 3.0 许可的规定在此重印。

12 Skipping Callbacks

Just as with validations, it is also possible to skip callbacks. These methods should be used with caution, however, because important business rules and application logic may be kept in callbacks. Bypassing them without understanding the potential implications may lead to invalid data.

  • decrement
  • decrement_counter
  • delete
  • delete_all
  • find_by_sql
  • increment
  • increment_counter
  • toggle
  • touch
  • update_column
  • update_all
  • update_counters

12 跳过回调

与验证一样,也可以跳过回调。但是,应谨慎使用这些方法,因为重要的业务规则和应用程序逻辑可能会保留在回调中。在不了解潜在影响的情况下绕过它们可能会导致无效数据。

  • 递减
  • 递减计数器
  • 删除
  • 删除所有
  • find_by_sql
  • 增量
  • 增量计数器
  • 切换
  • 触碰
  • 更新列
  • 全部更新
  • 更新计数器

回答by workdreamer

This will skip your validations:

这将跳过您的验证:

vote.save(:validate => false)

more info here

更多信息在这里

To skipping your callbacks and validations, you can use, update_column v(3.1), or update_all

要跳过回调和验证,您可以使用 update_column v(3.1) 或 update_all

vote = Vote.first
vote.update_column(:subject, 'CallBacks')

Aparentlly this only works with ActiveRecord 3.1

显然这仅适用于 ActiveRecord 3.1

Or:

或者:

Vote.where('id = ?', YourID).update_all(:subject => 'CallBacks')

In the end you have also i finally option and this will skip everthing:

最后,您还有 i finally 选项,这将跳过所有内容:

execute "UPDATE votes SET subject = 'CallBacks' WHERE id = YourID"

OK the last one it's not so pretty.

好吧,最后一个不是那么漂亮。