How to combine two conditions in a where clause?

2020-05-22 15:17发布

I have the following:

time_range = (1.month.ago.beginning_of_month..1.month.ago.end_of_month)

Comment.where(:created_at => time_range).count

How can I add to the where clause with a statement like:

.where("user_id is not in (?)",[user_ids]).

How can I combine the two? Thanks

3条回答
狗以群分
2楼-- · 2020-05-22 15:37

if you want a "AND" conditional query, try this:

Comment.
  where(:created_at => time_range).
  where("user_id is not in (?)",[user_ids])

which will produce SQL like : select ... where ... AND ...

if you want the WHERE clause more complicated, such as: where ( a AND b) OR (c AND d), you have to combine the conditions into the clause yourself, e.g.

Comment.where("(a AND b ) OR (c AND d)")
查看更多
不美不萌又怎样
3楼-- · 2020-05-22 15:52
User.where(name: 'Joe', email: 'joe@example.com')
查看更多
Melony?
4楼-- · 2020-05-22 16:01
User.where(["name = ? and email = ?", "Joe", "joe@example.com"])

This will be fine.

查看更多
登录 后发表回答