相关疑难解决方法(0)

ActiveRecord:当方法名称作为符号传递时,不调用after_initialize

我注意到after_initialize当回调符号作为输入传递时,Rails不会触发回调.

下面的代码不起作用.

class User < ActiveRecord::Base
  after_initialize :init_data

  def init_data
    puts "In init_data"
  end

end
Run Code Online (Sandbox Code Playgroud)

以下代码有效.

class User < ActiveRecord::Base

  def after_initialize 
    init_data
  end

  def init_data
    puts "In init_data"
  end
end
Run Code Online (Sandbox Code Playgroud)

有人可以解释这种行为吗?

注1

ActiveRecord 文档说明以下内容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. 
Run Code Online (Sandbox Code Playgroud)

虽然有人说After_initialize需要明确实现,但我发现上段中的第二句含糊不清,即In that case, all of the callback types …

activerecord ruby-on-rails

9
推荐指数
1
解决办法
4074
查看次数

formtastic中date_helper的默认值

听起来很基本,我不能将date_helper默认为日期,如:

- semantic_form_for resource do |f|
  - f.inputs do
    = f.input :issued_on, :default => Date.today
  = f.buttons
Run Code Online (Sandbox Code Playgroud)

如果资源没有日期,上面只会呈现空白列.

我会理解任何我可能做错的指针.

ruby-on-rails formtastic

4
推荐指数
1
解决办法
4181
查看次数

标签 统计

ruby-on-rails ×2

activerecord ×1

formtastic ×1