mahu mahu - 1 year ago 46
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?


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:,