4

I want to sort my where condition result in the order i am passing the values in array . what i am doing is i have an array of ids

ids = [80, 20, 3, 91, 84, 90, 98, 97, 68, 99, 92, 73]

When i am passing this array to where condition like :

products = Product.where(id: ids)

its returning the result active record relation in different order (random order)something like :

=>[ 20 ,84, 3,98 , .............. ] 

(this is active record relation object i have mention only ids here )

But i want it to return the object in same order i am passing the the values like (In active record relation object not an array)

=> [80, 20, 3, 91, 84, 90, 98, 97, 68, 99, 92, 73]

How can i do this .

1
  • want to sort it by the order in which i am passing it into array not by id Commented Jun 26, 2015 at 7:33

5 Answers 5

5

I have got the solution of this just need to do

In product model inside product.rb file put :

 def self.order_by_ids(ids)
    order_by = ["case"]
    ids.each_with_index.map do |id, index|
      order_by << "WHEN id='#{id}' THEN #{index}"
    end
    order_by << "end"
    order(order_by.join(" "))
  end

And query will be :

products = Product.where(:id => ids).order_by_ids(ids)

Here ids will be array of id's

Sign up to request clarification or add additional context in comments.

Comments

1

Simply sort by using indices of your ids array:

products = Product.where(id: ids).sort_by { |prod| ids.index(prod.id) }

Plus it's database agnostic, and it's ok to do in Ruby as you won't have millions of ids in any case.

1 Comment

Actually this will return an array not active record relation.
0

The answer is for mysql only

There is a function in mysql called FIELD()

So you can do something like

products = Product.where(id: ids).order("field(id, #{ids.join ','})")

1 Comment

I am looking for postgress actually . is there any way to achieve same wit postgress .
0

In Postgres

hot_offer_ids = ["7081", "7083", "6917", "5075"]
values = ''
hot_offer_ids.each_with_index do |offer_id, index|
  values += ", " if index > 0
  values += %((#{offer_id}, #{index}))
end
hot_offers = Offer.joins("join (VALUES #{values}) as x(id, ordering) on offers.id = x.id").order("x.ordering")

Comments

0

You can use this gem (order_as_specified) that allows you to do native SQL ordering like this:

Model.where(id: ids).order_as_specified(id: ids)

Reference Answer : Link

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.