views:

21

answers:

1

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
A: 

I'm pretty sure that methods invoked by symbol need to be protected or private.

Edit: Yep, here's the Rails 3 documentation:

The method reference callbacks work by specifying a protected or private method available in the object

Raphomet
I tried making my callback method private, and I still got the same behavior.
KandadaBoggu