【问题标题】:How to fetch those records whose due date is 3 more days more from the current date in rails?如何在rails中获取到期日期比当前日期多3天的记录?
【发布时间】:2017-05-02 23:58:36
【问题描述】:

我正在尝试查询到期日期(日期时间数据类型)比当前日期多 3 天的记录。我想在到期日的 3 天之前将剩余的邮件发送给人们。为此,我正在编写此查询。

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

同样,我想发送剩余的邮件说你的截止日期已经结束,(在这种情况下,截止日期是过去的日期。)如果我们添加 3 天,它应该等于当天,我想为那些记录获取这些记录。简而言之,我想在到期日的 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')

【问题讨论】:

    标签: ruby-on-rails datetime where


    【解决方案1】:

    让我们在下面几行中写下您的查询:
    1、invoices = Invoice.where("status = ? AND due_date = ?", "unpaid", Date.today + 3.days )

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

    【讨论】:

      【解决方案2】:

      我不认为你可以在 mysql 中对 date 做一个“-”,不过我可能错了。但最简单的方法是让 Rails 自己用这样的查询来处理......

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-11
        • 1970-01-01
        • 2019-10-08
        • 1970-01-01
        相关资源
        最近更新 更多