ActiveRecord: Overwrite attribute setter

Posted by Moser on 17 Jun 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.