5

Got a strange problem:

Boolean field not getting saved on Heroku (works fine locally)

Details:

  Rails 2.3 on Heroku (bamboo-ree-1.8.7).

Migration

  def self.up
   add_column :users, :send_contact_emails, :boolean, :default => false
  end

On Heroku:

>> u = User.last
=> #<User id: 100, ......
>> u.send_contact_emails = true
=> true
>> u.save
=> true

>> x = User.last
=> #<User id: 100, ...
>> x.send_contact_emails
=> nil  <---------------------------- Why is this ?

When I do this locally (Postgresql 8.4), it works as expected.

Any ideas ?

EDIT:

Ran some tests directly on the DB:

>> ActiveRecord::Base.connection.execute("SELECT send_contact_emails from users where id = 100")[0]
=> {"send_contact_emails"=>nil}

>> ActiveRecord::Base.connection.execute("UPDATE users SET send_contact_emails=FALSE where id=100")
=> #<PGresult:0x7f76d7593580>

>> ActiveRecord::Base.connection.execute("SELECT send_contact_emails from users where id = 100")[0]
=> {"send_contact_emails"=>"f"}

So the problem is with Rails and not Postgresql...

4
  • not very helpful, but looks like a duplicate of stackoverflow.com/questions/7437789/… Commented Oct 17, 2011 at 10:43
  • Sadly the magic solution suggested in that question (rollback/re-migrate) did not work. Commented Oct 17, 2011 at 10:54
  • sorry to hear that - it did seem a long shot, but I thought maybe worth mentioning anyway Commented Oct 17, 2011 at 12:02
  • do you have the same Postgres on heroku and localy? Commented Oct 17, 2011 at 12:18

3 Answers 3

12

Make sure that you restart your app.

http://devcenter.heroku.com/articles/rake

Once you run migrations that add new columns, you have to manually restart your app on heroku with "heroku restart" so that Rails will pick up the changes.

Sign up to request clarification or add additional context in comments.

1 Comment

I was having an issue where boolean fields generated by simple_form were showing up as text fields on Heroku. This answer fixed that problem. Thanks!
2

Looks like the problem was on the RAILS side.

Once I manually updated the values to false:

ActiveRecord::Base.connection.execute("UPDATE users SET send_contact_emails=FALSE")

The problem disappeared.

(As if rails 2.3.10 was unable to handle 'nil' in boolean fields..)

1 Comment

In all fairness, I believe that nil should not be treated as true or false, as it is neither of them. It's a very general word Nil is a word commonly used to mean nothing or zero; it is one of several names for the number 0. - Does it mean it has no value or does it mean it should be false. (en.wikipedia.org/wiki/Nil)
-1

Boolean in Rails uses tinyint column type, so it's 1/0 on DB level side.

API

Class
ActiveRecord::ConnectionAdapters::MysqlAdapter < AbstractAdapter

emulate_booleans

By default, the MysqlAdapter will consider all columns of type tinyint(1) as boolean. If you wish to disable this emulation (which was the default behavior in versions 0.13.1 and earlier) you can add the following line to your environment.rb file:

  ActiveRecord::ConnectionAdapters::MysqlAdapter.emulate_booleans = false

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.