我有一些型号有after_save回调.通常这很好,但在某些情况下,比如在创建开发数据时,我希望在不运行回调的情况下保存模型.有没有简单的方法?类似于...

Person#save( :run_callbacks => false )

Person#save_without_callbacks

I looked in the Rails docs and didn't find anything. However in my experience the Rails docs don't always tell the whole st或y.

使现代化

I found a blog post that explains how you can remove callbacks from a model like this:

Foo.after_save.clear

I couldn't find where that method is documented but it seems to w或k.

推荐答案

此解决方案仅适用于Rails 2.

我刚刚调查了一下,我想我有一个解决办法.您可以使用两种ActiveRecord私有方法:

update_without_callbacks
create_without_callbacks

您必须使用send来调用这些方法.例如:

p = Person.new(:name => 'foo')
p.send(:create_without_callbacks)

p = Person.find(1)
p.send(:update_without_callbacks)

这绝对是你只想在控制台中或在做一些随机测试时使用的东西.希望这有帮助!

Ruby-on-rails相关问答推荐

如何判断是否定义了Rails模型?

查询Where Date.parse Rails

如何在生成的 HTML 本身中显示部分名称

组合 active_record 模型并按共享属性排序

GroupingError:错误:列必须出现在 GROUP BY 子句中或在聚合函数中使用

Heroku 应用程序上的远程 mysql 数据库

Rails - 如何使用子对象的嵌套属性和强参数填充父对象ID?

Rails 路由到唯一索引

FactoryGirl 和 Rspec 测试中 attributes_for 的含义

Rails 服务对象与 lib 类

Rails 3.1 Sprockets 需要指令 - 有没有办法排除特定文件?

如何在 Ruby on Rails 的内存缓存存储中列出键?

如何为模型添加属性?

快速添加链接[:notice]

Active Record has_many:通过删除一条关联记录

has_many :通过 class_name 和 foreign_key

如何设置 MiniTest?

find、where 和 find_by_id 有什么区别?

Ruby on Rails:Cucumber:我如何获取单个功能?

Rails:您已经激活了 rake 10.3.1,但您的 Gemfile 需要 rake 10.2.2 (Gem::LoadError)