# Aliasing AR attributes

**URL:** <https://discuss.rubyonrails.org/t/aliasing-ar-attributes/43491>\
**Category:** rubyonrails-talk\
**Created:** [June 15, 2009, 2:14pm UTC](https://discuss.rubyonrails.org/t/aliasing-ar-attributes/43491 "2009-06-15T14:14:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_Vielsmaier](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@Martin\_Vielsmaier](https://discuss.rubyonrails.org/u/Martin_Vielsmaier)\
**Post date:** [June 15, 2009, 2:14pm UTC](https://discuss.rubyonrails.org/t/aliasing-ar-attributes/43491/1 "2009-06-15T14:14:37Z")

</div>

Hey,

I'd like to add some "magic" functionality to some model by chain aliasing an attributes setter method like this: class Thing \< ActiveRecord::Base def registration\_with\_upcase=(r) &nbsp;&nbsp;&nbsp;&nbsp;self.registration\_without\_upcase=(r.upcase) &nbsp;&nbsp;end &nbsp;&nbsp;alias\_method\_chain "registration=", :upcase end p = Plane.new p.destroy Plane.class\_eval \<\<-eos &nbsp;&nbsp;def registration\_with\_upcase=(r) &nbsp;&nbsp;&nbsp;&nbsp;self.registration\_without\_upcase=(r.upcase) &nbsp;&nbsp;end &nbsp;&nbsp;alias\_method\_chain "registration=", :upcase eos

---

<div class="post-metadata">

**Author:** ![Martin\_Vielsmaier](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@Martin\_Vielsmaier](https://discuss.rubyonrails.org/u/Martin_Vielsmaier)\
**Post date:** [June 15, 2009, 2:21pm UTC](https://discuss.rubyonrails.org/t/aliasing-ar-attributes/43491/2 "2009-06-15T14:21:56Z")

</div>

Argh sorry... Again: Hey,

I'd like to add some "magic" functionality to some model by chain aliasing an attributes setter method like this: class Thing \< ActiveRecord::Base def name\_with\_upcase=(r) &nbsp;&nbsp;&nbsp;&nbsp;self.name\_without\_upcase=(r.upcase) &nbsp;&nbsp;end &nbsp;&nbsp;alias\_method\_chain "name=", :upcase end

This (on script/console) gives me this error: NameError: /usr/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/ active\_support/core\_ext/module/aliasing.rb:33:in `alias_method': undefined method `name=' for class `Thing'

Seems like model doesn't know about its attributes at this point. Even if I add my aliasing code after the class: class Thing \< ActiveRecord::Base end Thing.class\_eval \<\<-eos &nbsp;&nbsp;def name\_with\_upcase=(r) &nbsp;&nbsp;&nbsp;&nbsp;self.name\_without\_upcase=(r.upcase) &nbsp;&nbsp;end &nbsp;&nbsp;alias\_method\_chain "name=", :upcase eos

I get the same error. But if I add a simple puts Thing.new between the class and the eval part, it works.

Any ideas how I can force the model to load it's attributes earlier?

---

<div class="post-metadata">

**Author:** ![Martin\_Vielsmaier](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@Martin\_Vielsmaier](https://discuss.rubyonrails.org/u/Martin_Vielsmaier)\
**Post date:** [June 17, 2009, 1:29pm UTC](https://discuss.rubyonrails.org/t/aliasing-ar-attributes/43491/3 "2009-06-17T13:29:28Z")

</div>

Ok, found a solution: (I didn't know about write\_attribute) This works: def name=(str) &nbsp;&nbsp;str.upcase! unless str.nil? &nbsp;&nbsp;write\_attribute(:name, str) end
