AR Model Initialization Notes

written by nap on March 16th, 2007 @ 10:03 AM

If you're new to Rails but not to Ruby, you might be surprised to learn that Rails doesn't use the standard Ruby initialize() method when Model.new is invoked or when a model instance is returned from a find.

If you need to add some initialization code to an ActiveRecord model, use the after_initialze callback instead:

def after_initialize
  @thing = SomethingElse.new(self)
  @foo = 'bar'
end

Comments are closed