after_initialize& after_find回调顺序在Active Record对象生命周期中? [英] after_initialize & after_find callbacks order in Active Record object life cycle?

查看:86
本文介绍了after_initialize& after_find回调顺序在Active Record对象生命周期中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自Rails指南。回调可能会涉及Active Record Object的生命周期。按照执行顺序(从Rails Guides复制):

From the Rails Guides. Callbacks could hook into Active Record Object's life cycle. In the order of execution, they're (copied from Rails Guides):


  • 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

  • 验证前

  • after_validation

  • before_save

  • around_save

  • before_update

  • around_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

  • before_destroy

  • around_destroy

  • after_destroy

  • after_commit / after_rollback

  • before_destroy
  • around_destroy
  • after_destroy
  • after_commit/after_rollback

我想知道将 after_initialize放在哪里 after_find 到上面?我认为 after_initialize 应该放在 before_validation 之前,而 after_find 不应该放在属于其中任何三个。我对么?谢谢。

I am wondering where to put the after_initialize and after_find into above? I think after_initialize should put before before_validation and after_find does not belongs to any three of them. Am I correct? Thanks.

推荐答案

after_initialize after_find 回调是两个特殊的回调。

The after_initialize and after_find callbacks are the two special callbacks.

after_find 和 after_initialize 事件将它们定义为方法。如果您尝试将它们声明为处理程序,它们将被忽略。

The only way to define callbacks for the after_find and after_initialize events is to define them as methods. If you try declaring them as handlers,they’ll be silently ignored.

来自 API


对象,都会触发> after_find after_initialize 回调。
after_initialize 在实例化新对象后也会触发

after_find and after_initialize callback is triggered for each object that is found and instantiated by a finder, with after_initialize being triggered after new objects are instantiated as well.

来自指南


每当实例化
Record对象时,都会调用 after_initialize 回调,直接使用new或从数据库加载
记录时。避免需要
直接覆盖您的Active Record初始化方法很有用。

The after_initialize callback will be called whenever an Active Record object is instantiated, either by directly using new or when a record is loaded from the database. It can be useful to avoid the need to directly override your Active Record initialize method.

after_find 每当Active Record从数据库加载
a记录时,都会调用回调。如果同时定义了 after_find ,则在
之前调用 after_initialize

The after_find callback will be called whenever Active Record loads a record from the database. after_find is called before after_initialize if both are defined.

after_initialize after_find 回调没有 before_ *
对应对象
,但可以像其他活动
Record回调一样注册它们。

The after_initialize and after_find callbacks have no before_* counterparts, but they can be registered just like the other Active Record callbacks.

class User < ActiveRecord::Base
  after_initialize do |user|
    puts "You have initialized an object!"
  end

  after_find do |user|
    puts "You have found an object!"
  end
end

>> User.new
You have initialized an object!
=> #<User id: nil>

>> User.first
You have found an object!
You have initialized an object!
=> #<User id: 1>


在何处放置<$ c是在AR对象生命周期中使用$ c> after_initialize after_find

由于它们与所有其他回调不同,并且它们没有 before_ *对应对象,因此作者(指指南此处的作者)可能有兴趣将它们分开,因为它们是特例。

Since they are different from all other callbacks and also they don't have before_* counterparts,so the author(referring to Guides author here) might be interested in putting them separately as they are special case.

最后,我同意将 after_initialize 放在 before_validation 之前。可能是这样。

And finally I would agree with in putting after_initialize before before_validation. It might be the case.

这篇关于after_initialize&amp; after_find回调顺序在Active Record对象生命周期中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆