[英]How to find records with 2 not condition in rails where clause?
I have a users table like this:我有一个像这样的用户表:
--------------------
| id | name | role |
--------------------
| ...| ... | ... |
--------------------
The role column has 3 kinds of role: Admin, Buyer, and User.角色栏有 3 种角色:Admin、Buyer 和 User。 With convention below:约定如下:
role != 'buyer' and role != 'admin' ==> User is a normal user
( This means role can have value: nil, "", or manny different values, except 'buyer' and 'admin' ) (这意味着角色可以有值:nil、"" 或许多不同的值,除了 'buyer' 和 'admin' )
role = 'buyer' ==> User is a buyer
role = 'admin' ==> User is an admin
Now, I want to find all the users in all 3 scenarios:现在,我想在所有 3 个场景中找到所有用户:
@admins = User.where(role: 'admin').all (work fine!)
@buyers = User.where(role: 'buyer').all (work fine!)
And all normal users, but this seems not working:和所有普通用户,但这似乎不起作用:
@users = User.where("role!='buyer' and role!='admin'").all (not working! )
Can anyone help me find all the normal users?谁能帮我找到所有的普通用户? Thanks in advance!提前致谢!
For all rails version,对于所有 Rails 版本,
User.where('role NOT IN (?)', ['buyer', 'admin'])
If you are using rails >= 4,如果您使用的是 Rails >= 4,
User.where.not(role: ['buyer', 'admin'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.