Home > Mobile >  How to write query in Ruby to check certain time object is less than 1 hour or 3 hour
How to write query in Ruby to check certain time object is less than 1 hour or 3 hour

Time:11-18

I have a Trip model in ruby which has start_at column and i need to get Trip object if start_at less than 1 hour or 3 hour (only i should get trip object when start_at is < 1hr or < 3hr not for < 2hr, < 4hr, ..... etc).

Note: I have Cron job which runs for every 15.minutes to get trip object as explained above.

Example: Assume i have a trip which start_at = 9:00 am and current time is 10:00 am i should get that trip object. Same goes for 3 hour also (start_at = 9:00 am and current time is 12:00 am)

^^ Except above two cases i should not get trip object, need to get only for less than 1hr or 3hr

This is what i tried

Trip.where("start_at < ? ", 1.hour.ago)

But above query returning trip object even if start_at < 2 hours ago, 4 hours ago, ..... blah blah

I am new to Ruby any help would be appreciated. Thanks!

CodePudding user response:

Trip.where(start_at: 2.hours.ago..1.hour.ago).or(Trip.where(start_at: 4.hours.ago..3.hour.ago))

CodePudding user response:

You could use or query, or use array value:

Trip.where(start_at: [2.hours.ago..1.hour.ago, 4.hours.ago..3.hour.ago])
  • Related