# Get a collection using an array of ids, keeping the order

**URL:** <https://discuss.rubyonrails.org/t/get-a-collection-using-an-array-of-ids-keeping-the-order/37012>\
**Category:** rubyonrails-talk\
**Created:** [December 4, 2008, 12:24pm UTC](https://discuss.rubyonrails.org/t/get-a-collection-using-an-array-of-ids-keeping-the-order/37012 "2008-12-04T12:24:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [December 4, 2008, 12:24pm UTC](https://discuss.rubyonrails.org/t/get-a-collection-using-an-array-of-ids-keeping-the-order/37012/1 "2008-12-04T12:24:07Z")

</div>

Say if i have an array of resource ids, of size n. Is there a way to get all the corresponding resources, without doing n individual Find calls?

eg, i could do this:

@resources = array.each{|id| @resources \<\< Resource.find(id)}

But, this requires n Find calls which isn't acceptable.

Any ideas anyone?

thanks max

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [December 4, 2008, 12:31pm UTC](https://discuss.rubyonrails.org/t/get-a-collection-using-an-array-of-ids-keeping-the-order/37012/2 "2008-12-04T12:31:59Z")

</div>

> Say if i have an array of resource ids, of size n. Is there a way to get all the corresponding resources, without doing n individual Find calls?
> 
> eg, i could do this:
> 
> @resources = array.each{|id| @resources \<\< Resource.find(id)}
> 
> But, this requires n Find calls which isn't acceptable.

Resource.find @resources

Fred

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [December 4, 2008, 12:36pm UTC](https://discuss.rubyonrails.org/t/get-a-collection-using-an-array-of-ids-keeping-the-order/37012/3 "2008-12-04T12:36:26Z")

</div>

> > Say if i have an array of resource ids, of size n. Is there a way to get all the corresponding resources, without doing n individual Find calls?
> > 
> > eg, i could do this:
> > 
> > @resources = array.each{|id| @resources \<\< Resource.find(id)}
> > 
> > But, this requires n Find calls which isn't acceptable.
> 
> Resource.find @resources

Sorry, only read the body of the message, not the subject! I would sort it after, eg

order\_hash = {} array.each\_with\_index {|id, index| order\_hash[id]=index Resource.find(array).sort\_by {|r| order\_hash[r.id]}

Fred

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [December 4, 2008, 12:46pm UTC](https://discuss.rubyonrails.org/t/get-a-collection-using-an-array-of-ids-keeping-the-order/37012/4 "2008-12-04T12:46:05Z")

</div>

Frederick Cheung wrote:

---

<div class="post-metadata">

**Author:** ![rab](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/rab/32/710_2.png) [@rab](https://discuss.rubyonrails.org/u/rab)\
**Post date:** [December 4, 2008, 1:59pm UTC](https://discuss.rubyonrails.org/t/get-a-collection-using-an-array-of-ids-keeping-the-order/37012/5 "2008-12-04T13:59:16Z")

</div>

YMMV, but why bother with the hash? Unless you have a truly huge list of id's

@resources = Resource.find(array).sort\_by {|r| array.index(r.id)}

You could even hide the implementation:

class Resource &nbsp;&nbsp;&nbsp;def self.find\_by\_ordered\_ids(\*array) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;options = {} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;options = array.pop if array.last.is\_a? Hash &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# pass an Array or a list of id arguments &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array = array.flatten if array.first.is\_a? Array &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;find(array, options).sort\_by {|r| array.index(r.id)} &nbsp;&nbsp;&nbsp;end end

(Note, untested, but that doesn't mean that it \*won't\* work 😉

-Rob

Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com) Rob@AgileConsultingLLC.com

---

<div class="post-metadata">

**Author:** ![Mark\_James](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@Mark\_James](https://discuss.rubyonrails.org/u/Mark_James)\
**Post date:** [December 5, 2008, 12:56am UTC](https://discuss.rubyonrails.org/t/get-a-collection-using-an-array-of-ids-keeping-the-order/37012/6 "2008-12-05T00:56:27Z")

</div>

If you use MySQL, the "field" function allows you to retrieve records in a specific id order:

[http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/158dc2d879b2fb1/af78ce75ddfa1ed1](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/158dc2d879b2fb1/af78ce75ddfa1ed1)

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [December 5, 2008, 9:23am UTC](https://discuss.rubyonrails.org/t/get-a-collection-using-an-array-of-ids-keeping-the-order/37012/7 "2008-12-05T09:23:09Z")

</div>

Mark Reginald James wrote:

> If you use MySQL, the "field" function allows you to retrieve records in a specific id order:
> 
> [http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/158dc2d879b2fb1/af78ce75ddfa1ed1](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/158dc2d879b2fb1/af78ce75ddfa1ed1)
> 
> -- Rails Wheels - Find Plugins, List & Sell Plugins - [http://railswheels.com](http://railswheels.com)

Ah, now that is a great tip, i'd not seen this before! Thanks a lot.

The weird thing is, i've gone to my console to try it out, and rails is now always giving me the resources back in the order of the id array anyway, whether i use the field option or not. It's the same with any of my AR classes! I'm sure that this didn't used to happen though...i think i'm going crazy. Can anyone tell me if this is normal? If so then i just imagined this whole problem...

AModel.find([3,1,2]).collect(&:id) =\> [3,1,2]

or do you get

=\> [1,2,3]

?

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [August 23, 2010, 2:18am UTC](https://discuss.rubyonrails.org/t/get-a-collection-using-an-array-of-ids-keeping-the-order/37012/8 "2010-08-23T02:18:50Z")

</div>

Max Williams wrote:

> The weird thing is, i've gone to my console to try it out, and rails is now always giving me the resources back in the order of the id array anyway, whether i use the field option or not. It's the same with any of my AR classes! I'm sure that this didn't used to happen though...i think i'm going crazy. Can anyone tell me if this is normal?

You are not going crazy.

MySQL may, but is not required to, return the objects in the order of the array. So, the behavior will change without warning.

You still need to sort after the fetch.

---

<div class="post-metadata">

**Author:** ![Michael\_Pavling](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Michael\_Pavling](https://discuss.rubyonrails.org/u/Michael_Pavling)\
**Post date:** [August 23, 2010, 7:38am UTC](https://discuss.rubyonrails.org/t/get-a-collection-using-an-array-of-ids-keeping-the-order/37012/9 "2010-08-23T07:38:18Z")

</div>

I am! This is a reply to a two-year-old thread... I just wasted 10 minutes trying to find out why I hadn't received the previous messages. I thought my email spam filter was being paranoid :-/
