【问题标题】:Time calculations in Rails where statementRails where 语句中的时间计算
【发布时间】:2012-10-22 10:09:27
【问题描述】:

我正在尝试在 where 语句中进行一些时间计算。我想选择生命周期已经过去的报价。

select_offers = Offer.where { (published_time + life_time) < DateTime.now }

它不会抛出任何错误,但也不会返回对象。我正在使用squeel gem。最好的方法是什么?

【问题讨论】:

  • 你的life_time的数据类型是什么?
  • 它是 int 但在视图中 published_time + life_time 工作正常并添加天数。在 statemet life_time.days 不起作用的地方。
  • 您确定您使用的幅度正确吗?我相信当 rails 比较两种不同格式的日期类型时,它会将日期转换为自纪元以来的秒数

标签: ruby-on-rails ruby-on-rails-3 datetime squeel


【解决方案1】:

假设life_time 是报价有效的天数。

select_offers = Offer.where(["DATE_ADD(published_time, INTERVAL life_time DAY) <  ? " , DateTime.now ]

Ref:-DATE_ADDmysql 函数

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-02
    • 2023-03-04
    • 1970-01-01
    • 2016-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多