Rails 3 DateTime Comparison w/ Date In An ActiveRecord Query

前端 未结 4 1328
死守一世寂寞
死守一世寂寞 2021-02-09 00:58

I\'m trying to search a model for any dates equal to a specific date while omitting the timestamp. In Rails I could simply execute this as DateTime.to_date == somedate

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-09 01:50

    With scope:

    scope :on_day, (lambda do |day|
       where(date: day.beginning_of_day..day.end_of_day)
    end)
    

    Using:

    Foo.on_day(Date.today).count
    

提交回复
热议问题