如何跳过ActiveRecord回调?

发布于 2024-08-02 16:02:46 字数 466 浏览 1 评论 0原文

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

我有这样的模型

class Vote < ActiveRecord::Base  
    after_save :add_points_to_user

    .....
end

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

Possible Duplicate:
How can I avoid running ActiveRecord callbacks?

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_user when saved? Possibly something like ActiveRecord#delete vs ActiveRecord#destroy?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遥远的绿洲 2024-08-09 16:02:46

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

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

..之后您可以在禁用 :add_points_to_user 的情况下对投票实例进行操作

For Rails 3, ActiveSupport::Callbacks gives 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:

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

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

时间你老了 2024-08-09 16:02:46

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

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

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

12 次跳过回调

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

  • 递减
  • 减量计数器
  • 删除
  • 全部删除
  • 通过_sql查找
  • 增量
  • increment_counter
  • 切换
  • 触摸
  • 更新列
  • 更新全部
  • 更新计数器

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

http://guides.rubyonrails.org/v3.2.13/active_record_validations_callbacks.html#skipping-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.

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
情场扛把子 2024-08-09 16:02:46

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

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

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

object.send(:create_without_callbacks)
object.send(:update_without_callbacks)
悲念泪 2024-08-09 16:02:46

这将跳过您的验证:

vote.save(:validate => false)

更多信息此处

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

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

显然这只适用于 ActiveRecord 3.1

或者:

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

最后你还有 i Final 选项,这将跳过一切:

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

好吧,最后一个它不太漂亮。

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

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

Aparentlly this only works with 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:

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

OK the last one it's not so pretty.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文