On Sep 23, 11:27 pm, MaggotChild <[email protected]> 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"
Fred
> #config/initializers/ar_attributes.rb
>
> module ActiveRecord
> module AttributeMethods
>
> alias_method :ar_read_attribute, :read_attribute
> def read_attribute(attr_name)
> p "read_override"
> ar_read_attribute(attr_name)
> end
>
> alias_method :ar_write_attribute, :write_attribute
> def write_attribute(attr_name, value)
> raise 'You made it!'
> end
> end
> end
>
> In the Rails console:
>
> >> person.read_attribute :name
>
> "read_override"
> => "Joe"
>
> >> person.write_attribute :name, "Bilal"
> => "Bilal"
> >> person.read_attribute :name
>
> "read_override"
> => "Bilal"
--~--~---------~--~----~------------~-------~--~----~
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 [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-talk?hl=en
-~----------~----~----~----~------~----~------~--~---