0

I have this query that I want to reduce down if possible:

@orders_in_progress = user.orders.where("state = ? OR state = ? OR state = ?, "Waiting for Payment Authorization", "Paid", "Shipped")

What I'm looking for is something like:

@orders_in_progress = user.orders.where("state = ?", "Waiting for Payment Authorization" || "Paid" || "Shipped)

Also, I'm a little confused on the terminology here - is this "an ActiveRecord query with multiple conditions for an element"?

cadlac
  • 2,802
  • 3
  • 18
  • 34

1 Answers1

4

You could do something like this:

@orders_in_progress = user.orders.where(state: ["Waiting for Payment Authorization", "Paid", "Shipped"])

It will generate "in" query but it will work the same way that your original one does.

Also you may want to consider changing states to something simpler ('pending', 'paid', 'shipped' come to mind) and use translations to generate human friendly version.

  • Just be careful if the array is empty, AR does [bizarre things with empty arrays](http://stackoverflow.com/q/12946004/479863). – mu is too short Feb 24 '13 at 23:18
  • @andrius perfect thanks! And I just came up with an easy to understand example, and was trying to make it "human friendly". I'm much simpler and cryptic in my code XD – cadlac Feb 24 '13 at 23:43