ActiveRecord的:犯规时,方法名是通过作为一个符号调用after_initialize [英] ActiveRecord: Doesnt call after_initialize when method name is passed as a symbol

查看:130
本文介绍了ActiveRecord的:犯规时,方法名是通过作为一个符号调用after_initialize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,Rails没有触发 after_initialize 回调,回调符号作为输入传递。

在code以下无法正常工作。

 类用户的LT;的ActiveRecord :: Base的
  after_initialize:init_data

  高清init_data
    提出在init_data
  结束

结束
 

在code以下工作。

 类用户的LT;的ActiveRecord :: Base的

  高清after_initialize
    init_data
  结束

  高清init_data
    提出在init_data
  结束
结束
 

有人可以解释这种现象?

注1

ActiveRecord的文档说以下关于 after_initialize

 与所有其他的回调,after_find和after_initialize意志
只有当运行一个明确的实现定义(DEF after_find)。
在这种情况下,所有的回调类型的将被调用。
 

虽然它说,after_initialize需要明确的实现,我觉得第二句上一段暧昧的,即在这种情况下,所有的     回调类型会被调用。什么是所有的呼叫回类型

在文档中的code样品具有不使用显式实现的例子:

  after_initialize EncryptionWrapper.new
 

解决方案

按照<一个href="http://guides.rubyonrails.org/active_record_validations_callbacks.html#after_initialize-and-after_find">documentation,你不能使用宏风格的类方法为 after_initialize after_find 回调:

  

在after_initialize和after_find   回调是从一个有点不同   其他。他们没有before_ *   同行,唯一的办法   注册它们是通过定义它们作为   常规的方法。如果您尝试   注册after_initialize或   after_find采用宏观样式类   方法,它们将只被忽略。   这种现象是由于性能   原因,因为after_initialize和   after_find都将被要求   在数据库中找到的每个记录,   显著减缓   查询。

总之,你必须定义一个 after_initialize 实例方法:

 类用户的LT;的ActiveRecord :: Base的

  高清after_initialize
    do_stuff
  结束

结束
 

I noticed that Rails doesn't trigger after_initialize callback when the callback symbol is passed as input.

The code below doesn't work.

class User < ActiveRecord::Base
  after_initialize :init_data

  def init_data
    puts "In init_data"
  end

end

The code below works.

class User < ActiveRecord::Base

  def after_initialize 
    init_data
  end

  def init_data
    puts "In init_data"
  end
end

Can somebody explain this behavior?

Note 1

The ActiveRecord documentation says the following about after_initialize:

Unlike all the other callbacks, after_find and after_initialize will 
only be run if an explicit implementation is defined (def after_find). 
In that case, all of the callback types will be called. 

Though it is stated that after_initialize requires explicit implementation, I find the second sentence in the above paragraph ambiguous, i.e. In that case, all of the callback types will be called. What is all of the call back types?

The code sample in the documentation has an example that doesn't use explicit implementation:

after_initialize EncryptionWrapper.new

解决方案

According to the documentation, you cannot use the macro-style class methods for the after_initialize or after_find callbacks:

The after_initialize and after_find callbacks are a bit different from the others. They have no before_* counterparts, and the only way to register them is by defining them as regular methods. If you try to register after_initialize or after_find using macro-style class methods, they will just be ignored. This behaviour is due to performance reasons, since after_initialize and after_find will both be called for each record found in the database, significantly slowing down the queries.

In short, you have to define an after_initialize instance method:

class User < ActiveRecord::Base

  def after_initialize
    do_stuff
  end

end

这篇关于ActiveRecord的:犯规时,方法名是通过作为一个符号调用after_initialize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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