I am using a mixin to add some functionality to my model (Person). In the mixin I need some initializations to be done so I am trying to use the “after_initialize” callback macro to invoke an initialization method. The model (Person) is only a base class for some other models.
The problem I am having is that it gets never called. I tried to debug it but the breakpoint never got hit. Also logging gives me no output.
I couldn`t find any help (as this construct should be working in Rails 3 according to the Api docs and some posts here).
/lib/mymodule.rb
module MyModule
after_initialize :generate_ids
def generate_ids
logger.info "invoked" #never hit
end
end
/models/person.rb
require "mymodule"
class Person < ActiveRecord::Base
include MyModule
end
/models/customer.rb
class Customer < Person
# nothing so far
end
*/controllers/customers_controller.rb (action => new)*
# GET /customers/new
# GET /customers/new.json
def new
@person = Customer.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @customer }
end
end
Please be indulgent to me as I am a “newbie” to RoR.
Thank you very much !
Best regards,
Thomas
UPDATE
After restarting the local app server it gives me the following exception:
ActionController::RoutingError (undefined method `after_initialize’ for SequentialRecord:Module):
I assume this callback can`t be used in mixins ?
Try something like that (not tested).