This project is archived and is in readonly mode.

#812 ✓committed
Tim Haines

_changed? doesn't work for attribute aliases

Reported by Tim Haines | August 13th, 2008 @ 12:21 AM | in 2.x

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

Comments and changes to this ticket

  • Tim Haines

    Tim Haines August 13th, 2008 @ 12:24 AM

    • 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.

  • Rich Cavanaugh
  • Rich Cavanaugh

    Rich Cavanaugh August 16th, 2008 @ 02:56 AM

    • 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.

  • Tim Haines

    Tim Haines August 18th, 2008 @ 12:09 AM

    +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.

  • Xavier Noria

    Xavier Noria August 18th, 2008 @ 12:38 AM

    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.

  • José Valim
  • Pratik

    Pratik August 22nd, 2008 @ 01:25 PM

    • Assigned user set to “Michael Koziarski”
  • Michael Koziarski

    Michael Koziarski August 30th, 2008 @ 10:59 AM

    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?

  • Rich Cavanaugh

    Rich Cavanaugh September 13th, 2008 @ 04:40 AM

    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.

  • Repository

    Repository September 13th, 2008 @ 10:51 AM

    • 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 state:committed] http://github.com/rails/rails/co...

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages