This project is archived and is in readonly mode.
_changed? doesn't work for attribute aliases
-
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.
-
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.
-
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.
-
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.
-
José Valim
Well done! +1
-
Pratik
- Assigned user set to 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?
-
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.
-
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...
