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:
Could I achieve the same result as above with
def after_initialize
self.Status = ‘G’
end
?
Due to the specialities about how ActiveRecord descendants are
created, is it somehow “better” to use after_initialize vs. overriding
initialize directly?
Can anyone share some other useful application of after_initialize
besides doing attribute initialization?
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.”