This project is archived and is in readonly mode.
after_initialize callback included from a module isn't fired
-
Pete Deffendol
After some experimentation, the following produces the desired result, although I still do not know why the original sample would fail:
module Common class Callbacks def self.after_initialize(record) puts "after_initialize" end end def self.included(base) base.instance_eval do after_initialize Callbacks end end end class User < ActiveRecord::Base include Common endI was under the impression, that "for performance reasons," only the explicit definition of "after_initialize" was allowed. The above use of the callback class seems to contradict that. Perhaps I am reading the documentation incorrectly? (http://edgeguides.rubyonrails.org/active_record_validations_callbac...)
-
José Valim
- State changed from new to wontfix
- Importance changed from to Low
In Rails 3, calling methods with the same name as the callback was deprecated. That said, you need to something like this:
module UserCallbacks def self.included(base) base.after_initializer :some_method end def some_method # end end -
Pete Deffendol
That's good to know, however, it looks like the documentation needs to be updated. Looking at http://github.com/rails/rails/blob/master/activerecord/lib/active_r... the part about an explicit implementation is misleading if not plain wrong.
-
Rohit Arondekar
Pete, if you would like to do it, the API Docs and the Guides can be edited here http://github.com/lifo/docrails You'll have to ask lifo (on Github) for write permissions and then you can edit the docs. The changes will be merged with the main Rails repo later on.