如何在rails Active Record查询中添加第二个条件

时间:2017-07-05 17:36:26

标签: ruby-on-rails ruby-on-rails-4 activerecord rails-activerecord ruby-on-rails-5

我想做一些像SQL AND这样的事情来为此添加第二个条件:

Article
  .where("date_trunc('day', created_at) = ?", Date.yesterday-(params[:increment].to_i))
  .order('articles.created_at DESC')

第二个条件是'site_id' attribute = "HIDE"

提前感谢任何提示。

2 个答案:

答案 0 :(得分:2)

只需添加具有所需条件的另一个where子句:

Article
  .where("date_trunc('day', created_at) = ?", Date.yesterday-(params[:increment].to_i))
  .where(site_id: 'HIDE')
  .order('articles.created_at DESC')

答案 1 :(得分:1)

您可以在AND声明中使用where

Article
  .where("date_trunc('day', created_at) = ? AND site_id = 'HIDE'", Date.yesterday-(params[:increment].to_i))
  .order("articles.created_at DESC")