Ruby-on-rails ActiveRecord 回调和验证的顺序是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10969673/
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
What is the order of ActiveRecord callbacks and validations?
提问by Abid
I was wondering in what order are callbacks and validations called when an ActiveRecord object is created.
我想知道在创建 ActiveRecord 对象时调用回调和验证的顺序是什么。
Let's say I have some custom validations & callbacks like the following:
假设我有一些自定义验证和回调,如下所示:
validates :reference_code, :if => :reference_code, :on => :create
before_create :assign_reference
which one will run first? The callback needs to happen first or else the validation may fail.
哪个会先运行?回调需要先发生,否则验证可能会失败。
回答by Bart Jedrocha
The most-up-to-date version of this list for the latest version of Rails can be found in the ActiveRecord::Callbacksdocumentation. The lists for Rails 4, 3 & 2 are below.
最新版本的 Rails 列表的最新版本可以在ActiveRecord::Callbacks文档中找到。Rails 4、3 和 2 的列表如下。
Rails 4
导轨 4
The most up-to-date version of this list can be found in the Rails 4 Guides.
该列表的最新版本可以在Rails 4 Guides 中找到。
Creating an object
创建对象
before_validationafter_validationbefore_savearound_savebefore_createaround_createafter_createafter_saveafter_commit/after_rollback
before_validationafter_validationbefore_savearound_savebefore_createaround_createafter_createafter_saveafter_commit/after_rollback
Updating an object
更新对象
before_validationafter_validationbefore_savearound_savebefore_updatearound_updateafter_updateafter_saveafter_commit/after_rollback
before_validationafter_validationbefore_savearound_savebefore_updatearound_updateafter_updateafter_saveafter_commit/after_rollback
Destroying an object
销毁对象
before_destroyaround_destroyafter_destroyafter_commit/after_rollback
before_destroyaround_destroyafter_destroyafter_commit/after_rollback
Rails 3
导轨 3
The most up-to-date version of this list can be found in the Rails 3 Guides.
该列表的最新版本可以在Rails 3 指南中找到。
Creating an object
创建对象
before_validationafter_validationbefore_savearound_savebefore_createaround_createafter_createafter_save
before_validationafter_validationbefore_savearound_savebefore_createaround_createafter_createafter_save
Updating an object
更新对象
before_validationafter_validationbefore_savearound_savebefore_updatearound_updateafter_updateafter_save
before_validationafter_validationbefore_savearound_savebefore_updatearound_updateafter_updateafter_save
Destroying an object
销毁对象
before_destroyaround_destroyafter_destroy
before_destroyaround_destroyafter_destroy
Rails 2
导轨 2
The most up-to-date version of this list can be found in the Rails 2.3 Guides
此列表的最新版本可以在Rails 2.3 指南中找到
Creating an object
创建对象
before_validationbefore_validation_on_createafter_validationafter_validation_on_createbefore_savebefore_createINSERToperationafter_createafter_save
before_validationbefore_validation_on_createafter_validationafter_validation_on_createbefore_savebefore_createINSERT手术after_createafter_save
Updating an object
更新对象
before_validationbefore_validation_on_updateafter_validationafter_validation_on_updatebefore_savebefore_updateUPDATEoperationafter_updateafter_save
before_validationbefore_validation_on_updateafter_validationafter_validation_on_updatebefore_savebefore_updateUPDATE手术after_updateafter_save
Destroying an object
销毁对象
before_destroyDELETEoperationafter_destroy
before_destroyDELETE手术after_destroy
Since you need to first validate the reference_code, the assign_referencemethod can be called in the after_validationcallback or any callback appearing after it in the list I provided above.
由于您需要首先验证reference_code,因此assign_reference可以在after_validation回调或我上面提供的列表中出现在它之后的任何回调中调用该方法。
回答by Deepsystm
Rails 5
导轨 5
Here is a list with all the available Active Record callbacks, listed in the same order in which they will get called during the respective operations:
这是一个包含所有可用 Active Record 回调的列表,按照它们在相应操作期间被调用的相同顺序列出:
1 Creating an Object
1 创建对象
before_validationafter_validationbefore_savearound_savebefore_createaround_createafter_createafter_saveafter_commit/after_rollback
before_validationafter_validationbefore_savearound_savebefore_createaround_createafter_createafter_saveafter_commit/after_rollback
2 Updating an Object
2 更新对象
before_validationafter_validationbefore_savearound_savebefore_updatearound_updateafter_updateafter_saveafter_commit/after_rollback
before_validationafter_validationbefore_savearound_savebefore_updatearound_updateafter_updateafter_saveafter_commit/after_rollback
3 Destroying an Object
3 销毁对象
before_destroyaround_destroyafter_destroyafter_commit/after_rollback
before_destroyaround_destroyafter_destroyafter_commit/after_rollback
after_saveruns both on create and update, but always after the more specific callbacks after_createand after_update, no matter the order in which the macro calls were executed.
after_save在创建和更新时运行,但总是在更具体的回调after_create和 之后运行after_update,无论宏调用的执行顺序如何。
before_destroycallbacks should be placed before dependent: :destroyassociations (or use the prepend: true option), to ensure they execute before the records are deleted by dependent: :destroy.
before_destroy回调应该放在dependent: :destroy关联之前(或使用 prepend: true 选项),以确保它们在记录被删除之前执行dependent: :destroy。

