5

遇到一个奇怪的问题:

布尔字段未保存在 Heroku 上(在本地工作正常)

细节:

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

移民

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

在 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 ?

当我在本地执行此操作(Postgresql 8.4)时,它按预期工作。

有任何想法吗 ?

编辑:

直接在数据库上运行一些测试:

>> 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"}

所以问题出在Rails 而不是Postgresql ......

4

3 回答 3

12

确保重新启动您的应用程序。

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

运行添加新列的迁移后,您必须使用“heroku restart”在 heroku 上手动重新启动您的应用程序,以便 Rails 接收更改。

于 2012-01-11T06:51:48.297 回答
2

看起来问题出在RAILS方面。

一旦我手动将值更新为 false:

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

问题消失了。

(好像 rails 2.3.10 无法处理布尔字段中的“nil”..)

于 2011-10-17T12:12:48.190 回答
-1

Rails 中的布尔值使用 tinyint 列类型,因此在 DB 级别侧为 1/0。

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
于 2011-10-17T12:05:34.013 回答