goo
goo

Reputation: 2280

Rails: Where Date.today is past OR is the :deadline_date

Basically, I'm a little stuck here. I'm using Rails 3.x.x.. The code I'm using now is as follows

@email_reminder = ToDo.where(:reminder => true, :deadline_date =< Date.today)

The code above obviously won't work. I googled around but almost every solution was a Rails 2.x.x. solution. What's the most efficient way of finding all my "email_reminders" if today's date is past OR is the deadline_date?

(doesn't have to be similar to method used above but it would help if you provide a way using a similar method)

Thanks in advance

Upvotes: 2

Views: 4396

Answers (2)

Noah Koch
Noah Koch

Reputation: 831

I see no reason to install a gem to fix this one issue. If you use some good 'ol SQL :)

@email_reminder = ToDo.where(:reminder => true).where('deadline_date <= ?', Date.today)

Note the order of the less than or equal to.

Granted if you run into issues like this a lot, Squeel might be worth looking into. However, I tend to stray away from libraries that use the word 'hackery'

Upvotes: 9

Tyler DeWitt
Tyler DeWitt

Reputation: 23576

To do OR statements with Rails, I always use squeel.

Should be something like

@email_reminder = ToDo.where{ (:reminder => true) | (:deadline_date =< Date.today) }

EDIT:

I've never seen the comma used to separate the two parts of the query (not sure that's possible), I would do it like this (2 where clauses are joined by AND in ActiveRecord:

 @email_reminder = ToDo.where(:reminder => true).where({ deadline_date <= Date.today })

Without squeel it would be something like

.where("deadline_date <= ?", Date.today)

Also, notice I flipped the = and < sign in the query

Upvotes: -3

Related Questions