2

I want to do a query like this:

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

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

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

0

2 Answers 2

1

Try convert your datetime field to date via SQL:

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

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

Note: it does not works in PotsgreSQL

Or:

bookings = Booking.where(user_id: @current_user.id,
                         created_at: date.beginning_of_day..date.end_of_day)
Sign up to request clarification or add additional context in comments.

Comments

0

How about this?

bookings = Booking.where(user_id: @current_user.id, start_at: start_date..end_date)

or

bookings = Booking.where(user_id: @current_user.id)
                  .where('start_at > ? AND start_at < ?', start_date, end_date)

where start_date and end_date are the two dates you wish to check between.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.