> > But 'after_initialize' implies "call this after you call the
> > instance's initialize method", and it's apparent that, in the case of
> > object retrieval, the initialize method simply isn't called (which
> > makes sense, because the object is only initialized once, when it got
> > created by calling its class's new method.)
>
> This thin and literal interpretation of the word 'initialize' makes
> sense only to those who care deeply about the implementation of AR.
> If we were to refactor Base.instantiate(record) to use .new, I'm not
> sure that many users would care, nor would they expect different
> callbacks to be called.
I really do have to take issue with this; I would argue that the
current behaviour of after_initialize is based on a 'thin and literal
interpretation of the word "initialize"'. Decanting an object from
storage is not initialization; initialization is what happens when an
object is first created, back before it got poured into the storage
jar.
Consider a woollen gansey's life cycle. It starts off as a length of
wool which is then knitted (initialized) on circular needles into a
delightful seemless garment. During cold weather, I wear it a lot.
During warm weather, I put it in a drawer (storage). When it gets cold
again, I don't take the gansey out of the drawer and knit it again
from scratch.
Conceptually, that's what's happening with my active record object.
It's knitted, shoved into the drawer and pulled out again as needed.
It only gets knitted once. Of course, what _really_ happens is that my
'gansey' is stored in the teleporter's pattern buffers (okay, if you
insist, we'll call it a database) and reconstituted from an entirely
new set of atoms when I need it later. But that doesn't matter. As far
as the wearer is concerned, it's the same gansey.
Plus, there's the argument from orthogonality... If after_initialize
only fires when the gansey is first knitted, then I can add behaviour
that fires after knitting and drawer removal by doing:
after_find :do_something
after_initialize :do_something
But when after_initialize fires after find as well, I have to write
after_initialize do |obj|
if obj.new_record?
...
end
end
and pay the cost of the method call every time I get a gansey out of
the drawer anyway. No fun.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Core" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---