我有一个类客户
class Customer < ActiveRecord::Base
enum status: [:unconfirmed]
default_scope { order updated_at: :desc }
scope :unconfirmed, -> { where(status: 0) }
end
Schema 中的 Status 字段被定义为默认为 0 的整数。
在开发中,SQLite,一切正常,但在生产中,PostgresSQL,当我尝试运行 Customer.unconfirmed 时,我收到一个错误:
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...
谁能帮我看看这是怎么回事?
最佳答案
要使用枚举
功能,您必须需要制作enum field as integer .但是根据您得到的异常,很明显您将字段 status
设置为 boolean。
您可以检查生产服务器中的列类型。以 rails c production
打开您的 Rails 控制台。然后 Customer.columns_hash['status'].type
查看列的类型。
将新迁移添加到 change the column type从 boolean 到 integer,然后应用迁移。 然后你可以这样做:
scope :unconfirmed, -> { where(status: :unconfirmed) }
#or
scope :unconfirmed, -> { where(status: 0) }
关于ruby-on-rails - 获取 PG::UndefinedFunction: ERROR in production,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29489547/