Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

_changed? doesn't work for attribute aliases

#812

If you define an attribute alias like so:

alias_attribute :name_for_cache, :name

Then run the following:

u.name = "bob" => "bob" u.name_changed? => true u.name_for_cache_changed? NoMethodError: undefined method name_for_cache_changed?' for #<User:0x12b25b8>

    from /Library/Ruby/Gems/1.8/gems/activerecord-2.1.0/lib/active_record/attribute_methods.rb:251:in `method_missing'
    from (irb):4

It seems like the alias_attribute could define the _changed? method also.

As a work around, I'm also aliasing _changed:

alias_attribute :name_for_cache_changed, :name_changed

Reported by Tim Haines · August 13th, 2008 @ 12:21 AM

State: committed
Milestone: 2.x
Assigned to: Michael Koziarski Michael Koziarski
Importance: none

Activity

  1. Tim Haines
    Tim Haines
    • Tag changed from activerecord, enhancement to 2.1, activerecord, edge, enhancement

    Opps, that formatting didn't work so well.

    I took a look at creating a patch for this, but was unsure if the alias_attribute method should simply define another method for _changed? (and possibly some of it's friends), or if the Dirty Module should be updated.

    August 13th, 2008 @ 12:24 AM

  2. Rich Cavanaugh
  3. Rich Cavanaugh
    Rich Cavanaugh
    • Tag changed from 2.1, activerecord, edge, enhancement to 2.1, activerecord, edge, enhancement, patch

    This issue caught my attention because attributes are commonly aliased to clean up the exposed API and having to use the original name to use the dirty tracking seemed clunky to me.

    I've attached a patch that handles this by overriding alias_attribute within the ActiveRecord::Dirty module. The new alias_attribute_with_dirty calls the original alias_attribute then loops through the method suffixes specific to the Dirty module and adds the appropriate aliases. I've also added tests for this functionality.

    August 16th, 2008 @ 02:56 AM

  4. Tim Haines
    Tim Haines

    +1

    Thanks for writing this patch Rich. I've learnt a lot from reading through it and reproducing it - my patch wouldn't have been this clean.

    August 18th, 2008 @ 12:09 AM

  5. Xavier Noria
    Xavier Noria

    Nice patch.

    I think that should be documented somewhere. Since alias_attribute does not appear in the official API perhaps it would be enough to say something at the top of dirty.rb.

    August 18th, 2008 @ 12:38 AM

  6. José Valim
  7. Pratik
    Pratik
    • Assigned user set to Michael Koziarski

    August 22nd, 2008 @ 01:25 PM

  8. Michael Koziarski
    Michael Koziarski

    Hey, nice work.

    I don't quite follow why you have to use the metaclass when you're doing alias_method_chain though, shouldn't you be able to hook into the extended method in ClassMethods and just call alias_method_chain from there?

    August 30th, 2008 @ 10:59 AM

  9. Rich Cavanaugh
    Rich Cavanaugh

    Thanks.

    I do agree that extended is a much better place to put the alias_method_chain and I've made that change.

    I did however maintain the metaclass approach because I've never seen a better way to amc class methods. I checked and other parts of rails are using the "class << ...end" approach which is what #metaclass is doing internally anyway. Just in case I remembered wrong, I did run through other approaches and they all resulted in errors.

    I readily admit I could be mistaken here though. Feel free to correct me. I've never been thrilled with having to open up the metaclass to do this anyway.

    September 13th, 2008 @ 04:40 AM

  10. Repository
    Repository
    • State changed from new to committed

    (from [113de01eaf48f64d2adf9f34d699e51619af616f]) Allow for the dirty tracking to work with the aliased name of aliased attributes.

    Signed-off-by: Michael Koziarski michael@koziarski.com [#812 _changed? doesn't work for attribute aliases state:committed] http://github.com/rails/rails/co...

    September 13th, 2008 @ 10:51 AM