Skip to main content

How to compare and write conditions on active record field "time"

General • Asked by sid d
 Fpclass(id: integer, name: string,  start_time: time)

 Fpclass.first.start_time
 # 2000-01-01 01:01:00 UTC

I'm having problem with comparing time field while fetching results as you've seen above its storing all date and time in database even though i mentioned Time field while creating. Does anyone had faced similar kind of problem before ? Can you share me how to approach with time fields in active record, or am i doing something wrong ?

Finally what i need ?

 Fpclass.where('start_time>?', Time.now.strftime("%H %M"))

[Edited]

So all the Time instances in Rails include a date with the Time. It will default to January 1st, 2000 as you can see but you can safely ignore that portion of the output.

You should be able to query the database just by doing this and it should ignore the Date portion of the time. Check the development logs for the SQL query to make sure it does.

 Fpclass.where('start_time>?', Time.zone.now)

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.