Purpose of after_initialize in ActiveRecord?

All,

I have a quick question - is the purpose of implementing
after_initialize to allow custom attribute initialization for AR
descendants?

I have been overriding initialize whenever I require custom setup of my
model classes, like so:

def initialize(attributes = nil)
super
self.Status = ‘G’
end

So I have a couple of questions:

  1. Could I achieve the same result as above with

def after_initialize
self.Status = ‘G’
end
?

  1. Due to the specialities about how ActiveRecord descendants are
    created, is it somehow “better” to use after_initialize vs. overriding
    initialize directly?

  2. Can anyone share some other useful application of after_initialize
    besides doing attribute initialization?

Thanks,
Wes

You should use after_initialize or you might find yourself running into
some weird errors.

What is the correct method visibility for after_initialize?

On 10/27/06, [email protected] [email protected] wrote:

You should use after_initialize or you might find yourself running into
some weird errors.

What kind of errors and why? Calling super before your own init code
seems logically equivelent to a filter and is more simple/OO. If
using good OO design principles breaks the framework (FYI, this hasn’t
bitten me) I’d argue the framework needs fixing. Either way, it’s
helpful to get an explanation so we all learn something rather than
just sying “don’t do that, it’s bad.”

Thanks,

pt.


Parker T.

510.541.0125

On 10/27/06, Wes G. [email protected] wrote:

What is the correct method visibility for after_initialize?

protected


Kent

See here