How to combine two conditions in a where clause?

前端 未结 3 677
梦毁少年i
梦毁少年i 2021-02-03 19:48

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
<
相关标签:
3条回答
  • 2021-02-03 20:11
    User.where(name: 'Joe', email: 'joe@example.com')
    
    0 讨论(0)
  • 2021-02-03 20:27

    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)")
    
    0 讨论(0)
  • 2021-02-03 20:32
    User.where(["name = ? and email = ?", "Joe", "joe@example.com"])
    

    This will be fine.

    0 讨论(0)
提交回复
热议问题