Rails 4 Postgres查询错误

时间:2016-06-17 20:42:41

标签: sql postgresql ruby-on-rails-4 methods model

这是我的代码,错误在WHERE

附近发生错误
first_of_month = Date.current.beginning_of_month
last_of_month = Date.current.end_of_month
today = Date.current
query = quotes.where('close_date BETWEEN ? AND ? AND status_id = 8 WHERE quote_exp < ? ', first_of_month, last_of_month, today)

query.sum(:total)

3 个答案:

答案 0 :(得分:1)

根据您的解释性说明,您可以像这样编写查询:

query = query.where(<<-EOQ, first_of_month, last_of_month, today)
  close_date BETWEEN ? AND ?
  AND (quote_exp >= ? OR status_id = 8)
EOQ

这将为您提供:(1)月份(2)中未过期或状态为8的所有内容8.请注意A->B也是~AvB

答案 1 :(得分:0)

您的第4行不应包含WHERE关键字。如果您打算将其他标准添加到其余标准,则应将其替换为AND,如下所示:

query = quotes.where('close_date BETWEEN ? AND ? AND status_id = 8 AND quote_exp < ? ', first_of_month, last_of_month, today)

答案 2 :(得分:0)

你可以用这种方式写得更好。

query = quotes.where(close_date: first_of_month..last_of_month, status_id: 8).where('quote_exp < ?', today).sum(:total)