I have a rather misterious problem dealing with booleans in Rails, here is how to reproduce:
- rails new boolean_bug
- rails generate model User verified:boolean
With this you should have an empty project with the User model.
3 Inside boolean_bug/app/models/user.rb
'
class User < ActiveRecord::Base
before_save :set_false
attr_accessible :verified
private
def set_false
self.verified = false
end
end
4 In the console know reproduce:
$rails -v
Rails 3.0.1
$ rails console --sandbox
ruby-1.9.2-p0 > User.create!
ActiveRecord::RecordNotSaved: ActiveRecord::RecordNotSaved
from /Users/dpalacio/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/persistence.rb:56:in `save!'
from /Users/dpalacio/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/validations.rb:49:in `save!'
from /Users/dpalacio/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/attribute_methods/dirty.rb:30:in `save!
5 Change self.verified = false
to self.verified = 0
6 Go to the console again
ruby-1.9.2-p0 > User.create!
=> #<User id: 1, verified: false, created_at: "2010-10-31 04:23:13", updated_at: "2010-10-31 04:23:13">
So the point is that saving using false does not work, but true, 1, 0 do work, is this a bug ? Or am I doing something wrong ?