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

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

What is the order of ActiveRecord callbacks and validations?

ruby-on-railsruby-on-rails-3validationactiverecord

提问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_validation
  • after_validation
  • before_save
  • around_save
  • before_create
  • around_create
  • after_create
  • after_save
  • after_commit/after_rollback
  • before_validation
  • after_validation
  • before_save
  • around_save
  • before_create
  • around_create
  • after_create
  • after_save
  • after_commit/after_rollback

Updating an object

更新对象

  • before_validation
  • after_validation
  • before_save
  • around_save
  • before_update
  • around_update
  • after_update
  • after_save
  • after_commit/after_rollback
  • before_validation
  • after_validation
  • before_save
  • around_save
  • before_update
  • around_update
  • after_update
  • after_save
  • after_commit/after_rollback

Destroying an object

销毁对象

  • before_destroy
  • around_destroy
  • after_destroy
  • after_commit/after_rollback
  • before_destroy
  • around_destroy
  • after_destroy
  • after_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_validation
  • after_validation
  • before_save
  • around_save
  • before_create
  • around_create
  • after_create
  • after_save
  • before_validation
  • after_validation
  • before_save
  • around_save
  • before_create
  • around_create
  • after_create
  • after_save

Updating an object

更新对象

  • before_validation
  • after_validation
  • before_save
  • around_save
  • before_update
  • around_update
  • after_update
  • after_save
  • before_validation
  • after_validation
  • before_save
  • around_save
  • before_update
  • around_update
  • after_update
  • after_save

Destroying an object

销毁对象

  • before_destroy
  • around_destroy
  • after_destroy
  • before_destroy
  • around_destroy
  • after_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_validation
  • before_validation_on_create
  • after_validation
  • after_validation_on_create
  • before_save
  • before_create
  • INSERToperation
  • after_create
  • after_save
  • before_validation
  • before_validation_on_create
  • after_validation
  • after_validation_on_create
  • before_save
  • before_create
  • INSERT手术
  • after_create
  • after_save

Updating an object

更新对象

  • before_validation
  • before_validation_on_update
  • after_validation
  • after_validation_on_update
  • before_save
  • before_update
  • UPDATEoperation
  • after_update
  • after_save
  • before_validation
  • before_validation_on_update
  • after_validation
  • after_validation_on_update
  • before_save
  • before_update
  • UPDATE手术
  • after_update
  • after_save

Destroying an object

销毁对象

  • before_destroy
  • DELETEoperation
  • after_destroy
  • before_destroy
  • DELETE手术
  • 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_validation
  • after_validation
  • before_save
  • around_save
  • before_create
  • around_create
  • after_create
  • after_save
  • after_commit/after_rollback
  • before_validation
  • after_validation
  • before_save
  • around_save
  • before_create
  • around_create
  • after_create
  • after_save
  • after_commit/after_rollback

2 Updating an Object

2 更新对象

  • before_validation
  • after_validation
  • before_save
  • around_save
  • before_update
  • around_update
  • after_update
  • after_save
  • after_commit/after_rollback
  • before_validation
  • after_validation
  • before_save
  • around_save
  • before_update
  • around_update
  • after_update
  • after_save
  • after_commit/after_rollback

3 Destroying an Object

3 销毁对象

  • before_destroy
  • around_destroy
  • after_destroy
  • after_commit/after_rollback
  • before_destroy
  • around_destroy
  • after_destroy
  • after_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