0

I'm working on an event system where admins can select events to feature and add promotional text. As they need to add additional text the featured events are in their own table and reference the event details.

I'm now trying to output featured events for each day in the next week. Simplified example:

day = DateTime.now.to_date
featured_events = @featured_events.where('event.start_datetime = ?', day)

The problem is that start_datetime is in datetime format and day is in date format so it always outputs nothing. Is it possible to compare these two values with .where()?

Thanks!

samlester
  • 335
  • 3
  • 18
  • try something like `Try created_at >= some_day_with_00:00:00 timestamp and create_at < some_day_plus_one_with_00:00:00 timestamp` – Nitin Jain Feb 13 '14 at 16:21

2 Answers2

0

You don't say what database you're using.

In PostgreSQL we'd truncate the datetime inside the DB query so the DBM can do the compare. On MySQL we'd use a function to convert the datetime to a date.

Community
  • 1
  • 1
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • Thanks for the info – I'm using Postgres. Could you give an example of how that would work in the above code? – samlester Feb 13 '14 at 16:22
0

May be you're looking for this:

@featured_events.where('event.start_datetime > ? and event.start_datetime < ?', DateTime.now, 1.day.from_now.beginning_of_day)
Danil Speransky
  • 29,891
  • 5
  • 68
  • 79