Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Active Record unable to save if boolean is set to false

#5900

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!'<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

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

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Denis Odorcic
    Denis Odorcic
    • 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.

    October 31st, 2010 @ 05:12 AM

  2. Daniel Palacio
    Daniel Palacio

    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

    October 31st, 2010 @ 05:15 AM

  3. Santiago Pastorino
    Santiago Pastorino
    • 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.

    February 2nd, 2011 @ 04:21 PM

  4. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:21 PM