Reputation: 2119
I am trying to create a scope where checkin_time (DateTime) is added with duration (Int) in hours which is greater than or equal to Time.now
So something like this
scope :current, -> { where('checkin_time + duration.hours >= ?', Time.now) }
However, the string 'checkin_time + duration.hours >= ?'
above is not valid. How can I achieve something that will give me the correct result?
Upvotes: 0
Views: 99
Reputation: 434615
The easiest thing to do with PostgreSQL is to convert your duration
to an interval
and add that interval
to your timestamp. Something like this:
where("checkin_time + (duration || 'hours')::interval >= ?", Time.now)
or even:
where("checkin_time + (duration || 'hours')::interval >= now()")
The duration || 'hours'
builds a string like '6hours'
and then the ::interval
casts that string to an interval
which can be added to a timestamp.
Upvotes: 2