Heroku在Rails中读取字符串而不是整数?

时间:2013-05-12 10:10:37

标签: ruby-on-rails postgresql heroku cedar

在使用Postgres版本9.1.9的Heroku Cedar堆栈上运行的Rails 3.2.2应用程序中 执行以下方法时:

class Post < ActiveRecord::Base
  def shared_to_all
    privacy & 1 == 1
  end
end

我遇到了这个错误:

Completed 500 Internal Server Error in 19ms

NoMethodError (undefined method `&' for "0":String):
  app/models/post.rb:75:in `shared_to_all'
  app/controllers/application_controller.rb:212:in `next_post'

“privacy”在heroku数据库的DDL中定义为“int4 DEFAULT 0”。 该应用程序在本地使用相同的rails框架/ gems /数据库工作正常,甚至在使用heroku运行控制台时:

irb(main):008:0> Post.first.shared_to_all
=> false
irb(main):009:0> Post.first.privacy.class
=> Fixnum
irb(main):010:0> Post.first.privacy & 1 == 1
=> false

1 个答案:

答案 0 :(得分:1)

原来这可能只是数据库迁移中的一个小故障,因为我已经尝试回滚并再次迁移,现在它似乎工作正常。不知道是否应该删除或留下问题以防其他人遇到同样的问题。