2

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

goo
  • 2,230
  • 4
  • 32
  • 53
  • I'm not sure if there is a native `OR` operation in `ActiveRecord`. What example have you found? – Tyler DeWitt Jul 15 '13 at 20:10
  • Possible solution here: http://stackoverflow.com/a/5000839/2525797 (would need to be tweaked, obviously). – Eric Palace Jul 15 '13 at 20:14
  • @EricPalace One of the answers is "Model.where(:column => ["value", "other_value"]" -- how would I do this when using a "less than OR equal to" condition? – goo Jul 15 '13 at 21:02

2 Answers2

9

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'

Noah Koch
  • 831
  • 8
  • 10
-3

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

Tyler DeWitt
  • 23,366
  • 38
  • 119
  • 196
  • I would like for the "reminder =>" part to be constant. I want the "Or" statement for the date only (today's date 'is' OR 'past' the deadline_date) – goo Jul 15 '13 at 20:15
  • Typo in your code? `<=` instead of `=<`?. Also, updating code to reflect changes – Tyler DeWitt Jul 15 '13 at 20:18
  • I'm searching for all the "ToDos" that have a reminder boolean set to true and the deadline date is past Or is today's date. – goo Jul 15 '13 at 20:21
  • Sorry, still need `squeel`. Updating – Tyler DeWitt Jul 15 '13 at 20:24
  • 1
    So I think it was just a matter of separating the query into 2 `where` clauses and then getting the right syntax of `<=`? I'm pretty sure the `<=` means today's date OR before – Tyler DeWitt Jul 15 '13 at 20:28
  • I might just stick to the "column ?" way of doing things like this. Adding 2 seperate 'where' conditions seems unnecessary & I thought there was a simpler way like the code in my original question. Thanks anyway & I'll checkout "squeel." – goo Jul 15 '13 at 20:44