【问题标题】:Rails - filter datetime for time only in joined modelRails - 仅在加入模型中过滤日期时间以获取时间
【发布时间】:2017-03-19 09:37:02
【问题描述】:

我有两个模型:Route has_many Details

在控制器中,我将两个模型加入如下:

@routes = Route.where("origin_city_name LIKE (?)", "%#{@from}")
                   .destination_city_name(@to)
                   .company(@company)
                   .joins(:details)

Detail 有一个departure 日期时间属性。

我可以通过date 过滤如下:

@routes = @routes.where( details: { departure: @date..Float::INFINITY }) if params[:date]

我怎样才能只为time 做同样的事情?

【问题讨论】:

  • 你的意思只是departure的时间部分?
  • 是的,只有时间部分
  • 您可以使用details.departure::time 获取pg 中列的时间部分并从那里开始工作。

标签: ruby-on-rails datetime


【解决方案1】:

如果你使用的是 Postgresql 查询会是这样的:

@routes.where("departure::time > ?", '13:20:00')

【讨论】:

  • 现在无法测试.. 但是,即使离开是一列详细信息,您确定这会起作用吗?
  • 您可以在查询中指定表:@routes.where("details.departure::time > ?", '13:20:00')。但它也应该在没有表格的情况下工作,除非我的查询中只有一个 departure 列。
猜你喜欢
  • 1970-01-01
  • 2013-10-06
  • 2014-03-31
  • 1970-01-01
  • 2022-08-22
  • 1970-01-01
  • 2018-12-02
  • 1970-01-01
  • 2012-07-10
相关资源
最近更新 更多