2016-12-17 51 views
0

我想查詢其due_date(datetime數據類型)比當天多3天的記錄。我想在他們到期日的3天之前將剩餘的郵件發送給人們。因爲我正在寫這個查詢。如何在rails中從當前日期獲取截止日期多於3天的記錄?

remind_invoices = Invoice.where("status = ? AND (due_date - 3) == ?", "unpaid", Date.today) 

同樣地,我想送其餘郵件說你的截止日期已經過去,(在這種情況下,截止日期是過去的日期。)我想對那些如果我們增加3天,768,16等於獲取這些記錄到當天。簡單地說,我想在截止日期的3天后發送電子郵件。爲此我目前的查詢是這樣的。

unpaid_invoices = Invoice.where("status = ? AND (due_date + 3) == ?", "unpaid", Date.today) 

Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '== '2016-12-17')' at line 1: SELECT `invoises`.* FROM `invoises` WHERE (status = 'unpaid' AND (due_date - 3) == '2016-12-17') 
ActiveRecord::StatementInvalid: Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '== '2016-12-17')' at line 1: SELECT `invoises`.* FROM `invoises` WHERE (status = 'unpaid' AND (due_date - 3) == '2016-12-17') 

回答

0

但這些查詢是給錯誤的,我不認爲你可以做一個「 - 」上的日期在MySQL中,我可能是錯的,一個雖然。但最簡單的方法,讓Rails的本身處理與這樣的查詢...

remind_invoices = Invoice.where(status: 'unpaid').where('due_date <= ?', 3.days.from_now)

0

讓我們寫查詢下同線:
1,發票= Invoice.where(「狀態= AND DUE_DATE =「」,「未付」,Date.today + 3.days)

2,unpaid_invoices = Invoice.where(「status =?AND due_date =?」,「unpaid」,Date.today - 3.days )

相關問題