Aliasing AR attributes

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)     self.registration_without_upcase=(r.upcase)   end   alias_method_chain "registration=", :upcase end p = Plane.new p.destroy Plane.class_eval <<-eos   def registration_with_upcase=(r)     self.registration_without_upcase=(r.upcase)   end   alias_method_chain "registration=", :upcase eos

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)     self.name_without_upcase=(r.upcase)   end   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   def name_with_upcase=(r)     self.name_without_upcase=(r.upcase)   end   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?

Ok, found a solution: (I didn't know about write_attribute) This works: def name=(str)   str.upcase! unless str.nil?   write_attribute(:name, str) end