mahu mahu - 1 year ago 63
Ruby Question

Ruby format DateTimeParameter in where query

I want to do a query like this:

bookings = Booking.where(user_id:, start_at: date.to_datetime)

The problem in this case is, that I just get results in bookings which match a timestamp
which is quite logical.

Is there a possibility to format the
parameter in the query to date, so that I just compare two dates and not datetimes?

Answer Source

Try convert your datetime field to date via SQL:

bookings = Booking.where(user_id:
                  .where("DATE(start_at) = ?", date)

DATE() extracts the date part of a datetime expression.

Note: it does not works in PotsgreSQL


bookings = Booking.where(user_id:,
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download