简体   繁体   English

包含OR的where子句的活动记录中的正确语法

[英]Proper syntax in active record for where clause that contains an OR

Can anyone tell me the proper syntax for the following? 谁能告诉我以下的正确语法?

User.where(id_a: 2 OR id_b: 2).count

I'm trying to find the total number of users with either an id_a of 2 an or id_b of 2, but everything I try returns some error. 我试图找到id_a为2 an或id_b为2的用户总数,但我尝试的所有内容都会返回一些错误。

User.where("id_a = ? OR id_b = ?", 2, 2).count

If you don't want to repeat the same parameter 2 times and if the ? 如果你不想重复相同的参数2次,如果? makes you dizzy :), you can use the elegant "placeholder conditions" syntax described here (see section 2.2.1) as follows: 让你晕眩:),你可以使用这里描述的优雅的“占位符条件”语法(参见第2.2.1节),如下所示:

User.where("id_a = :user_id OR id_b = :user_id", user_id: 2).count

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM