4

In a Rails 3.2.2 app running on Heroku Cedar stack with Postgres version 9.1.9.
While executing the following method:

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

I run into this error:

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" is defined in the DDL of the heroku database as 'int4 DEFAULT 0'. The app works fine locally with the same rails framework/gems/database, and even when using heroku run console:

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
Claudio Floreani
  • 2,441
  • 28
  • 34
  • This has nothing to do with your question but, why do you use the bitwise operator, instead of the `zero?` method? – Pigueiras May 12 '13 at 10:22
  • @Pigueiras because privacy is a bitstring that I'm using instead of having a bunch of boolean columns. Each bit in privacy controls some permission. – Claudio Floreani May 12 '13 at 10:44
  • Not quite sure what you're doing, but why not use `privacy.present?` or `privacy.to_i` or something like that? – mind.blank May 12 '13 at 10:46
  • @mind.blank privacy is always present, it defaults to 0 and cannot be nil. I would typecast only if it turns out to be the best practice, at the moment it just seems very silly to cast an integer .to_i – Claudio Floreani May 12 '13 at 10:53
  • `&` and `&&` are two completely different things. The former is a bitwise operator, the latter is a logical operator. You cannot do `5 & true` or `"string" & true`, they don't have any meaning. – Claudio Floreani May 12 '13 at 11:01
  • They all work in the rails console and in the local production environment. – Claudio Floreani May 12 '13 at 17:59
  • I faced that issue after changing a default value on a migration and running update_all. When I ran a new migration removing the column and adding it back again, it solved the issue. The culprit, I think, was the update_all method – rapcal Apr 06 '14 at 07:22

1 Answers1

1

It turned out it may have been simply a glitch in the db migration, because I've tried to rollback and migrate again and now it seems to work fine. Don't know if the question should be deleted or left in case someone other runs into the same problem.

Claudio Floreani
  • 2,441
  • 28
  • 34