ActiveRecord oddity of the day

Yesterday I got bit by this fairly obscure behavior in ActiveRecord:

>> person = Person.first
=> #<Person id: 1, name: "Jakob", height: 170, created_at: "2010-01-22 08:57:02", updated_at: "2010-01-22 08:58:57">
>> person.height = 169,5
=> [169, 5]
>> person.height
=> 1

Uhm, huh? The above is from Rails 2.3.5 using SQLite, but I originally saw the issue running on PostgreSQL.

It turns out, that if the object you assign to an integer attribute/column does not respond to to_i, the attribute will be set to 1 – unless the object evaluates to false in which case the attribute will be set to 0.

My best guess is that ActiveRecord is trying to work around the lack of boolean columns in MySQL with this. So even though I am not using MySQL I still have to suffer its ineptitude. One just can’t win.