# Maintaining order when I call Video.find("3", "1", "2")

**URL:** <https://www.ruby-forum.com/t/maintaining-order-when-i-call-video-find-3-1-2/84556>\
**Category:** Rails\
**Created:** [February 6, 2007, 12:20am UTC](https://www.ruby-forum.com/t/maintaining-order-when-i-call-video-find-3-1-2/84556 "2007-02-06T00:20:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [February 6, 2007, 12:20am UTC](https://www.ruby-forum.com/t/maintaining-order-when-i-call-video-find-3-1-2/84556/1 "2007-02-06T00:20:34Z")

</div>

If I call Video.find(“3”, “1”, “2”), it’ll give me all the videos but  
the orders will be with ids 1, 2, and 3. I want to maintain the order  
that I pass it in. Is there a quick easy way to do that? Either  
sorting in the db or with Ruby.

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [February 6, 2007, 12:27am UTC](https://www.ruby-forum.com/t/maintaining-order-when-i-call-video-find-3-1-2/84556/2 "2007-02-06T00:27:35Z")

</div>

On Feb 5, 2007, at 5:17 PM, Pat M. wrote:

> If I call Video.find(“3”, “1”, “2”), it’ll give me all the videos but  
> the orders will be with ids 1, 2, and 3. I want to maintain the order  
> that I pass it in. Is there a quick easy way to do that? Either  
> sorting in the db or with Ruby.

I think your best bet is to separate them out into separate SQL  
statements. The connection adapter is translating that into something  
like:

SELECT … FROM videos WHERE id IN (3,1,2);

The result set from the database will most likely come out in DB-  
natural order, which probably works out to PK order. In any case, it  
is database dependent and I can’t think of an easy way to get the  
result set in a particular order. You can always do

ids.map {|id| Video.find(id)}

but of course it multiplies the number of queries by n.

Brad

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [February 6, 2007, 12:41am UTC](https://www.ruby-forum.com/t/maintaining-order-when-i-call-video-find-3-1-2/84556/3 "2007-02-06T00:41:35Z")

</div>

On 2/6/07, Pat M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If I call Video.find(“3”, “1”, “2”), it’ll give me all the videos but  
> the orders will be with ids 1, 2, and 3. I want to maintain the order  
> that I pass it in. Is there a quick easy way to do that? Either  
> sorting in the db or with Ruby.
> 
> Pat

For a quick stab

video\_ids = [3,1,2]  
videos\_temp = Video.find( \*video\_ids )  
videos = video\_ids.map{ |vid| videos\_temp.detect{ |video| video.id ==  
vid }  
}

Whoa that’s ugly.

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [February 6, 2007, 12:57am UTC](https://www.ruby-forum.com/t/maintaining-order-when-i-call-video-find-3-1-2/84556/4 "2007-02-06T00:57:36Z")

</div>

On Feb 5, 2007, at 6:40 PM, Daniel N wrote:

> For a quick stab
> 
> video\_ids = [3,1,2]  
> videos\_temp = Video.find( \*video\_ids )  
> videos = video\_ids.map{ |vid| videos\_temp.detect{ |video| video.id  
> == vid } }
> 
> Whoa that’s ugly.

Hopefully this isn’t:

> > ids = [36, 27, 35]  
> > =\> [36, 27, 35]  
> > products = Product.find ids  
> > =\> […stuff…]  
> > products.size  
> > =\> 3  
> > products.map(&:id)  
> > =\> [27, 35, 36]  
> > products.sort\_by {|p| ids.index(p.id)}.map(&:id)  
> > =\> [36, 27, 35]  
> > ids  
> > =\> [36, 27, 35]

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
