本文介绍了Rails的4.1枚举错误where条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
今天,我要改变我的Postgres枚举的Rails 4.1枚举。我做了一些步骤。
Today i want change my Postgres enum to Rails 4.1 enum.I make some steps.
迁移:
create_table :users do |t|
# other column here
t.column :status, :integer
end
模型:
class User < ActiveRecord::Base
enum status: [ :local, :tourist ]
end
现在创建用户:
=> User.create(email: '[email protected]', password: '12345678', status: 'local')
=> #<User id: "53621ec84d6163124", email: "[email protected]", status: 0, ......>
=> u = User.last
=> User Load (0.9ms) SELECT "users".* FROM "users" ........
=> #<User id: "53621ec84d6163124", email: "[email protected]", status: 0, ......>
=> u.local?
=> true
尝试找到本地用户(现在我有我的枚举奇怪的行为):
Try find local users(and now i have strange behavior of my enum):
=> User.where("status <> ?", User.statuses[:local])
=> User Load (0.9ms) SELECT "users".* FROM "users" WHERE (status <> 0)
=> []
跆拳道?
=> User.where("status <> ?", User.statuses[:tourist])
=> User Load (0.9ms) SELECT "users".* FROM "users" WHERE (status <> 1)
=> #<User id: "53621ec84d6163124", email: "[email protected]", status: 0, ......>
所以问题,这个查询 User.where(状态&LT;&GT;,User.statuses [:本地])
应该返回我的本地用户。还是我做错了什么?
So problem, this query User.where("status <> ?", User.statuses[:local])
should return my local user. Or i doing something wrong?
推荐答案
您在这里使用了错误的操作符。
You are using a wrong operator here.
"status <> ?" equals "status != ?"
相反,你要使用
Instead you want to use
"status = ?"
这篇关于Rails的4.1枚举错误where条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!