Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - Boolean field not saved on PostgreSQL / Heroku

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...

like image 394
Boris Avatar asked Oct 17 '11 10:10

Boris


1 Answers

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.

like image 191
onedanshow Avatar answered Sep 23 '22 20:09

onedanshow