Find() with id array - can i preserve the ordering?

Sort of a newbie question - sorry if it’s been asked before and I missed
it
in my search.

I have an id_array of Author ids, and I want to retrieve the associated
Author models as quickly as possible, but also preserve the ordering of
the
id_array.

Using Author.find(id_array) returns the records, but not in the same
order.
Is there some flag I can pass to find() to preserve the order? I want
to
avoid looping through the id array and finding the models individually.

Thanks in advance,
John

john li wrote:

I have an id_array of Author ids, and I want to retrieve the associated
Author models as quickly as possible, but also preserve the ordering of
the id_array.

Author.find(id_array).sort_by { |e| id_array.index(e.id) }


Josh S.
http://blog.hasmanythrough.com

Ah, I was wondering if there was some simple built-in flag to do it, but
that code snippet should definitely work too. Thanks!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs