msn email google-talk twitter tumblr flickr

Overriding Rails ActiveRecord object getter/setter fields

重载AR的属性方法 read_attribute/write_attribute存取被重载属性

   1      class Foo < ActiveRecord::Base
   2          def name
   3            reg = Regexp.compile('\b('+@@reg+')\b',Regexp::IGNORECASE)
   4  
   5            n = read_attribute(:name)
   6  
   7            n = n.gsub(reg,"")
   8            n = n.gsub(/^\s+|\s+$/,'')
   9          end
  10  
  11        def name=(name)
  12          write_attribute(:name,"#{name} - bar")
  13        end
  14      end