Rails find Conditions

Hi, I am trying to return all the records in my table for which a date
comparison reveals that that date is within 7 days. The code I have
written looks like this:

@subscriptions = find(:all, :conditions => [‘subscriptions.user_id = ?
AND subscriptions.confirmed = 1 AND (DATEDIFF(day,now(),events.start_on)
< 7)’,user_id], :joins => “INNER JOIN events on subscriptions.event_id =
events.id”)

However it throws a nasty SQL error and no matter what I do I can’t seem
to resolve it.

FYI the error is this:

Mysql::Error: #42000You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near ‘:day,now(),events.start_on) < 7))’ at line 1: SELECT

  • FROM subscriptions INNER JOIN events on subscriptions.event_id =
    events.id WHERE (subscriptions.user_id = 1 AND subscriptions.confirmed =
    1 AND (DATEDIFF(:day,now(),events.start_on) < 7))

Can anyone give me any clues?!

Much appreciated,

Mick

Hi, I am trying to return all the records in my table for which a date
comparison reveals that that date is within 7 days. The code I have
written looks like this:

@subscriptions = find(:all, :conditions => [‘subscriptions.user_id = ?
AND subscriptions.confirmed = 1 AND (DATEDIFF(day,now(),events.start_on)
< 7)’,user_id], :joins => “INNER JOIN events on subscriptions.event_id =
events.id”)

@subscriptions = find(:all,
:conditions => [‘subscriptions.user_id = ? AND subscriptions.confirmed =
1
AND events.start_on >= ?’, user_id, 7.days.ago.at_beginning_of_day],
:joins => “INNER JOIN events on subscriptions.event_id = events.id”)

-philip

Hi Philip, sorry for the late reply - that code works a treat!

Thanks for your help,

Mick