Andy
Andy

Reputation: 751

How to query by time in rails?

What can i do to query records by the time in a date time field? And by "time," i mean the time without the date. For example, I want every record that's created at midnight of any day.

Upvotes: 2

Views: 904

Answers (2)

Eric Sites
Eric Sites

Reputation: 1534

More like this:

Product.where("time(created_at) = time(?)", Time.new(2008,6,21, 24,0,0, '-00:00'))

EDIT

I added the timezone param to Time.new, that param should match the timezone you have your date time fields in your database.

Upvotes: 3

jdoe
jdoe

Reputation: 15771

Product.where("created_at like ?", "%00:00:00%")

Upvotes: 1

Related Questions