On 22/07/07, Michael Koziarski <[EMAIL PROTECTED]> wrote:
>
> > 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.
>
> I can buy your argument, but if I do so, there doesn't seem to be a
> need for after_initialize:
>
> def initialize(attrs)
>   do_stuff
>   super
> end
>
> All we'd be doing is reinventing an initialize method which hid the
> arguments.  Doesn't seem particularly necessary, especially at the
> cost of backwards compatibility for the people who use
> after_initialize at present.

Have you actually tried that? I have. It's a nightmare. It's long
enough ago now that I can't remember exactly what the nightmare was,
but it was definitely no fun at all.

A cursory examination ActiveRecord::Base suggests that it's because
the various attributes don't work until after '@attributes =
attributes_from_column_definition' has been evaluated, and that gets
evaluated when you call @super.

I accept that there are people who rely on the current behaviour of
after_initialize, so the trick will probably be to come up with a good
name for the callback. 'after_new'?

Another option might be to turn ActiveRecord::Base#initialize into a
template method along the lines of:

  def initialize(attributes = nil)
    @attributes = attributes_from_column_definition
    @new_record = true
    ensure_proper_type
    initialize_defaults if self.respond_to?(:initialize_defaults)
    self.attributes = attributes unless attributes.nil?
    yield self if block_given?
  end

which has the advantage of making the callback after the bones of the
object are in place, but before any @attributes are assigned to.

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to