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

后端 未结 3 631
甜味超标
甜味超标 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:34

    Keep in mind a DateTime holds a date and a time, so you're looking for records that have a precise value, not just the same day.

    You can do this one of two ways. You can either select the range of time from the beginning of the day to the end, or you can create a date column to help group your data better.

    The range version looks like this:

    @deals = Deal.where('start BETWEEN ? AND ?', DateTime.now.beginning_of_day, DateTime.now.end_of_day).all
    

    The other one requires creating a new start_date column in your table and populating it with the dates accordingly. You can index this date and have your queries run much faster, as range selections aren't always quick on large sets of data.

提交回复
热议问题