Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Date After

I need to do a query on Rails model time stamps (created_at and updated_at) finding all records after a specified date. When executing the query:

Item.where("created_at > :date OR updated_at > :date", date: "2011-05-29")
> SELECT "items".* FROM "items" WHERE (created_at > '2011-05-29' OR updated_at > '2011-05-29')

It includes records like:

attributes: 
  created_at: 2011-05-29 16:07:10.664227
  updated_at: 2011-05-29 16:07:10.664229

However, this date isn't greater than the one specified. Any simple way to fix this query? Thanks.

Edit:

I've also tried doing:

Item.where("created_at > :date OR updated_at > :date", date: "2011-05-29 16:07:10")

With the same result, however adding the fraction of seconds stamp as well gives the correct result.

like image 281
Kevin Sylvestre Avatar asked May 30 '11 00:05

Kevin Sylvestre


1 Answers

Say you want records starting 30th May:

Then search for records that are >= 2011-05-30 00:00:00:

some_date = Time.now
Item.where("created_at >= :date OR updated_at >= :date", date: some_date.tomorrow.beginning_of_day)
like image 75
Zabba Avatar answered Oct 12 '22 23:10

Zabba