On Sep 24, 12:34 am, Frederick Cheung <frederick.che...@gmail.com>
wrote:
> On Sep 23, 11:27 pm, MaggotChild <hsomob1...@yahoo.com> wrote:
>
> > Could someone explain this?
>
> At a quick glance it is probably because after AttributeMethods is
> included in ActiveRecord::Base, write_attribute is aliased &
> overwridden (eg the change tracking module). You then change
> write_attribute on AttributeMethods but it is too late - the aliasing
> that occured in Dirty is pointing at the previous implementation. When
> you alias a method ruby does keep track of what the aliased method was
> at the time alias_method was called, for example:
>
> class Foo
>   def to_be_aliased
>     "implementation 1"
>   end
>   alias_method :old_implementation, :to_be_aliased
> end
>
> class Foo
>   def to_be_aliased
>      "implementation 2"
>   end
> end
>
> Foo.new.old_implementation #=> "implementation 1"


Hi Fred,

Thanks for your response. I think you're on to something and I'll have
to take a look a the Dirty module (amongst others). But, even if the
module was aliasing the original write_attribute, I don't see how this
would interfere with my redefinition.

write_attribute is at the top of the call stack so I'd think it's
going to use the most recent definition. Unless some other module
redefined write_attribute within the scope of active AR::Base (as
apposed to including it via a module) -which is possible.


Using your example, I believe the case is more like the following:

class Foo
  def to_be_aliased
    p "implementation 1"
  end
  alias_method :old_implementation, :to_be_aliased
end

class Foo
  def to_be_aliased
    p "implementation 2"
  end
end

Foo.new.to_be_aliased #implementation 2

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to