4
在使用Postgres 9.1.9版的Heroku Cedar堆栈上运行的Rails 3.2.2应用程序。
当执行下面的方法:Heroku阅读字符串而不是整数在Rails中?
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'
“隐私” 在Heroku的数据库为 'INT4 DEFAULT 0' 的DDL定义。 的应用程序中使用的Heroku运行控制台时,正常工作与本地相同Rails框架/宝石/数据库,甚至是:
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
这与你的问题没有任何关系,但是,为什么你使用按位运算符而不是'zero?'方法? – Pigueiras 2013-05-12 10:22:39
@Pigueiras因为隐私是我使用的比特串,而不是有一堆布尔列。隐私中的每一位都控制着一些权限。 – 2013-05-12 10:44:29
不太清楚你在做什么,但为什么不使用'privacy.present?'或'privacy.to_i'或类似的东西? – 2013-05-12 10:46:26