Find Records with Datetime that match today's date - Ruby on Rails

后端 未结 3 566
甜味超标
甜味超标 2021-01-31 16:23

I have a table of deals and need to find records where the date matches today\'s date.

From the rails console, the date field I need to match looks like this. I\'ve assi

3条回答
  •  星月不相逢
    2021-01-31 16:46

    Rails 5.1 has introduced Date#all_day helper, which returns a range object for a given date, so you can just write:

    Deal.where(start: Date.today.all_day)
    

    You can also use SQL DATE() function for your goal, e.g.:

    @deals = Deal.where('DATE(start) = ?', Date.today)
    

提交回复
热议问题