This project is archived and is in readonly mode.

#5900 ✓stale
Daniel Palacio

Active Record unable to save if boolean is set to false

Reported by Daniel Palacio | October 31st, 2010 @ 05:06 AM

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

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!'<br/> from /Users/dpalacio/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.1/lib/active_record/validations.rb:49:insave!'
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!<br/> 5 Change self.verified = false to self.verified = 0

6 Go to the console again

ruby-1.9.2-p0 > User.create!
=> #

As you can see when we set the boolean to false it's unable to save, while if we set it to 0 or to true etc, save will work

Comments and changes to this ticket

  • Denis Odorcic

    Denis Odorcic October 31st, 2010 @ 05:12 AM

    • Tag set to invalid

    If your before_save function returns false, then rails won't save the object as it cancels all callbacks. You're assigning false to self.verified, which returns the value of self.verified which is false, so it doesn't save. Not really a bug, just how the before_* functions work.

    From the docs:

    If a before_* callback returns false, all the later callbacks and the associated action are cancelled. If an after_* callback returns false, all the later callbacks are cancelled. Callbacks are generally run in the order they are defined, with the exception of callbacks defined as methods on the model, which are called last.

  • Daniel Palacio

    Daniel Palacio October 31st, 2010 @ 05:15 AM

    Damn that was quick ! Anyway thanks, this has actually taken forever to debug and pinpoint repro.
    Anyway sorry for the confusion, thanks for the help

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:21 PM

    • State changed from “new” to “open”

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:21 PM

    • State changed from “open” to “stale”

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=""></a>

People watching this ticket