Tag: attribute setter

ActiveRecord: Overwrite attribute setter

Posted by on June 17, 2009

It’s somehow trivial, but I found out about ‘write_attribute‘ only just: [another case of rtfm]

class Thing < ActiveRecord::Base
  def name=(str)
    str.upcase! unless str.nil?
    write_attribute(:name, str)
  end
end

Update:
Be careful if you use this with Datetime attributes.