This project is archived and is in readonly mode.

#2616 ✓stale
Esben Mose Hansen

ActiveRecord.Base attribute accessor recommendation

Reported by Esben Mose Hansen | May 7th, 2009 @ 12:41 PM | in 3.x

The documentation for ActiveRecord::Base suggest overloading attribute accessors like this:

def length=(minutes)
  write_attribute(:length, minutes.to_i * 60)
end

Using this with update_attributes with { :length=>"6d" } will result in a silent conversion to 6, ignoring any validation. I haven't figured out the way to do this for integers (probably match against a regex) but for floats I'd use

def length=(minutes)

write_attributes(:length, Kernel.Float(minutes)*60.0)

recue ArgumentError

write_attributes(:length, minutes)

end

so that validation will still do their thing, etc.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages