0

I have a model

class Order < ActiveRecord::Base 
  ...
  enum status: [:started, :finished, :failed, :processing]
  ...
end

And want to select all processing and finished orders. I cannot write simply

@orders = Order.where(status:  [:finished, :processing])

because status field is naturally an integer, not string or literal. So this statement generates

2.2.0 :008 > Order.where(status: [:finished, :processing])
   SELECT "orders".* FROM "orders" WHERE "orders"."status" IN (NULL, NULL)  

Now i do the following

@orders = Order.where(status:  [:finished, :processing].map { |s| Order.statuses[s] }

Is there any better way?

2
  • Try @orders = Order.where(status: [1,3]) Commented Jun 1, 2016 at 5:52
  • Thank you, but it breaks abstractions. I want to call order statuses by it's names, not integers Commented Jun 1, 2016 at 5:54

1 Answer 1

2

As you want to call order statuses by it's names, the below should work

@orders = Order.where(status: Order.statuses[:finished, :processing])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.