_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 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 August 16th, 2008 @ 02:56 AM
- no changes were found...
-

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

-
Pratik August 22nd, 2008 @ 01:25 PM
- → Assigned user changed from to 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 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 September 13th, 2008 @ 10:52 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...
Please Login or create a free account to add a new comment.
You can update this ticket by sending an email to from your email client. (help)
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
Source available from github
The Git repository resides at http://github.com/rails
Check out the current development trunk (Edge Rails) with:
git clone git://github.com/rails/rails.git
Creating or reviewing a patch
See the contributor guide.
Creating a feature request
Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.
Creating a bug report
When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.
Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.
Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too".
