Embellishing String (or whatever) for AR attributes

Forgive my extreme noobishness, but why doesn't the following work?

class Wombat < ActiveRecord::Base   validates_presence_of :name

  class String     def normalize       self.upcase     end   end

  def name=(name)     write_attribute(:name, name.normalize)   end end

Of course I'd want to put the embellishment of String in a module, but I'm just trying to get it working. But instead I get:

w = Wombat.new(:name => "test")

NoMethodError: undefined method `normalize' for "test":String         from (irb):25:in `name='         from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/base.rb:1672:in `send'         from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/base.rb:1672:in `attributes='         from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/base.rb:1671:in `each'         from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/base.rb:1671:in `attributes='         from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/base.rb:1505:in `initialize_without_callbacks'         from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/ active_record/callbacks.rb:225:in `initialize'         from (irb):29:in `new'         from (irb):29

So what's the right way to do this? I just want to do better than:

  def name=(name)     write_attribute(:name, normalize(name))   end

class Wombat < ActiveRecord::Base validates_presence_of :name

class String    def normalize      self.upcase    end end

Move this String class block outside of the Wombat class block.

Eventually move it into say RAILS_ROOT/lib/local/string.rb and then add "require 'local/string'" to your environment.rb or some such.

-philip

Thanks. Perfect.