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

Ask A Question

Notifications

You’re not receiving notifications from this thread.

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

sid d asked in General
 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)
Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 33,665+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.