1

I have a class Customer

class Customer < ActiveRecord::Base
  enum status: [:unconfirmed]

  default_scope { order updated_at: :desc }

  scope :unconfirmed, -> { where(status: 0) }
end

Status field in Schema is defined as integer with a default 0.

In development, SQLite, all is working fine but in a production, PostgresSQL, when I try to run Customer.unconfirmed I am getting an error:

PG::UndefinedFunction: ERROR:  operator does not exist: boolean = integer
LINE 1: ...s".* FROM "customers"  WHERE "customers"."status" = 0  ORDER...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT "customers".* FROM "customers"  WHERE "customers"."status" = 0  ORDER BY "customers"."updated_at" DESC
ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR:  operator does not exist: boolean = integer
LINE 1: ...s".* FROM "customers"  WHERE "customers"."status" = 0  ORDER...

Can someone help me with finding out what is going on here?

Kocur4d
  • 6,701
  • 8
  • 35
  • 53

1 Answers1

1

To work with enum feature, you MUST need to make the enum field as integer. But as per the Exception you got, it is clear that you made the field status as boolean.

You can inspect the column type in your production server. Open your rails console as rails c production. Then Customer.columns_hash['status'].type to see the type of the column.

Add a new migration to change the column type from boolean to integer, and then apply the migration. Then you can do as :

scope :unconfirmed, -> { where(status: :unconfirmed) }
#or
scope :unconfirmed, -> { where(status: 0) }
Community
  • 1
  • 1
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
  • but status is set up as integer I have add_column :customers, :status, :integer, default: 0 in my migration file – Kocur4d Apr 07 '15 at 10:46
  • @Kocur4d See if there is an pending migration in your *Production* environment. – Arup Rakshit Apr 07 '15 at 10:46
  • @Kocur4d Do in your production after opening the console as `rails c production` --> `Customer.columns_hash['status'].type` to inspect its datatype.. – Arup Rakshit Apr 07 '15 at 10:55
  • It shows type as :boolean, I have no idea how this happen. My migration files are up to date and it is integer in there. What will be the best way to change it to integer via psql? – Kocur4d Apr 07 '15 at 11:04
  • @Kocur4d Well write another migration to change the [type](http://stackoverflow.com/questions/5191405/change-a-column-type-from-date-to-datetime-during-ror-migration). Don't edit any existing migration file. – Arup Rakshit Apr 07 '15 at 11:05
  • yes that was it, I have modified existing migration and this was the problem. Changing types for pg is not as straight forward as for sqllite it is not going to let you do simply chage :table, :column, :type. Thank you especially for Customer.columns_hash['status'].type – Kocur4d Apr 07 '15 at 11:32