Compare rails with dates

I have a data model that contains a DateTime attribute, a date. I want to find out if this date is today. I have hellish time!

>> Deal.first.date 
=> Mon, 14 Dec 2009 23:59:59 EST -05:00

I tried so many techniques that I would be embarrassed to put them here. So I would just ask what do I need to match this date in the ActiveRecord search method?

Thanks, Aaron.

+3
source share
3 answers

I guess the time zone leads you to the butt.

For a specific example, you can do:

 model.date.today?

To search for activerecord, try the following:

 Deal.all(:conditions=>['date > ?',Time.zone.now.beginning_of_day])

Here's the problem:

>> Time.zone.now.beginning_of_day == Date.today.beginning_of_day
=> false
+5
source

I would say something like this:

Deal.find(:first, :conditions => ['date >= ? AND date < ?', Date.today, Date.today+1])
0
source

, , , , .

Deal.first(:condition => { :date => Date.today })
-1

Source: https://habr.com/ru/post/1725667/


All Articles