# Order by array

**URL:** <https://www.ruby-forum.com/t/order-by-array/125466>\
**Category:** Rails\
**Created:** [January 9, 2008, 11:03am UTC](https://www.ruby-forum.com/t/order-by-array/125466 "2008-01-09T11:03:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![gorter9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gorter9](https://www.ruby-forum.com/u/gorter9)\
**Post date:** [January 9, 2008, 11:03am UTC](https://www.ruby-forum.com/t/order-by-array/125466/1 "2008-01-09T11:03:02Z")

</div>

I need the id’s order by a array.

def pages  
@admin = Admin.find(1)  
Page.find(:all, :order =\> [@admin.sidebar])  
end

when the array has  
2, 1, 3, 4, 5  
the output is 2, 3, 4, 5, 1  
and not the same as the array.  
How can i order the output the same as the array?

---

<div class="post-metadata">

**Author:** ![gorter9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gorter9](https://www.ruby-forum.com/u/gorter9)\
**Post date:** [January 9, 2008, 11:44am UTC](https://www.ruby-forum.com/t/order-by-array/125466/2 "2008-01-09T11:44:37Z")

</div>

i guess you could use the array to iterate through the collection (i’m  
assuming that @admin.sidebar is an array)  
e.g.  
\<%= @admin.sidebar.each do |i| %\>  
\<%= @pages[i].foo %\>  
\<% end %\>

maybe there’s a neater way

On Jan 9, 10:03 am, GA Gorter [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![gorter9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gorter9](https://www.ruby-forum.com/u/gorter9)\
**Post date:** [January 9, 2008, 12:49pm UTC](https://www.ruby-forum.com/t/order-by-array/125466/3 "2008-01-09T12:49:18Z")

</div>

You could use acts\_as\_list for this.

On Jan 9, 2008 9:13 PM, Wildtangent [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ## 2, 1, 3, 4, 5 the output is 2, 3, 4, 5, 1 and not the same as the array. How can i order the output the same as the array?
> > 
> > Posted viahttp://www.ruby-forum.com/.

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![gorter9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gorter9](https://www.ruby-forum.com/u/gorter9)\
**Post date:** [January 9, 2008, 3:18pm UTC](https://www.ruby-forum.com/t/order-by-array/125466/4 "2008-01-09T15:18:16Z")

</div>

On Jan 9, 2008, at 6:48 AM, Ryan B. wrote:

> > when the array has  
> > Ryan B.  
> > [http://www.frozenplague.net](http://www.frozenplague.net)  
> > Feel free to add me to MSN and/or GTalk as this email.

Or, assuming that the order is distinct for each Admin and  
@admin.sidebar contains a list of Page#id values, you could:

Page.find(:all).sort\_by { |page| @admin.sidebar.index(page.id) }

You might also consider having a separate model (called “Sidebar”  
perhaps?) that is something like:

class Sidebar \< ActiveRecord::Base  
belongs\_to :admin  
has\_many :pages, :order =\> :position  
acts\_as\_list :scope =\> :admin\_id  
end

which allows you to do:

class Admin  
has\_one :sidebar  
has\_many :pages, :through =\> :sidebar  
end

@admin.pages

(Warning: This is straight from me head and may contain typos or other  
problems)

-Rob

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

---

<div class="post-metadata">

**Author:** ![gorter9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gorter9](https://www.ruby-forum.com/u/gorter9)\
**Post date:** [January 11, 2008, 11:30am UTC](https://www.ruby-forum.com/t/order-by-array/125466/5 "2008-01-11T11:30:40Z")

</div>

I tried all the methods described her but i get no result

% for @pages in pages %\>  
32:  
33: \<% @admin.sidebar.each do |i| %\>  
34: \<%= @pages[i].foo %\>  
35:  
36: \<% end %\>  
37: \<% end %\>

wih this method i got the error

can’t convert String into Integer

How can i save a array into the database?

---

<div class="post-metadata">

**Author:** ![gorter9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gorter9](https://www.ruby-forum.com/u/gorter9)\
**Post date:** [January 11, 2008, 10:37am UTC](https://www.ruby-forum.com/t/order-by-array/125466/6 "2008-01-11T10:37:34Z")

</div>

acts\_as\_list is a undefined method.  
Also in the api i cant’t find acts\_of\_list  
Maybe it is not a method in rails 2.0 or something

---

<div class="post-metadata">

**Author:** ![gorter9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gorter9](https://www.ruby-forum.com/u/gorter9)\
**Post date:** [January 11, 2008, 1:32pm UTC](https://www.ruby-forum.com/t/order-by-array/125466/7 "2008-01-11T13:32:36Z")

</div>

Use acts\_as\_list, it’s a plugin that can be installed via script/plugin  
install acts\_as\_list. It’s exactly what you want.

On Jan 11, 2008 9:00 PM, GA Gorter [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 37: \<% end %\>
> 
> >

–  
Ryan B.

> **[frozenplague.net](http://www.frozenplague.net)**
>
> This domain may be for sale!

Feel free to add me to MSN and/or GTalk as this email.

---

<div class="post-metadata">

**Author:** ![gorter9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gorter9](https://www.ruby-forum.com/u/gorter9)\
**Post date:** [January 11, 2008, 3:09pm UTC](https://www.ruby-forum.com/t/order-by-array/125466/8 "2008-01-11T15:09:16Z")

</div>

Ryan B. wrote:

> Use acts\_as\_list, it’s a plugin that can be installed via script/plugin  
> install acts\_as\_list. It’s exactly what you want.
> 
> On Jan 11, 2008 9:00 PM, GA Gorter [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > 37: \<% end %\>
> > 
> > >
> 
> –  
> Ryan B.  
> [http://www.frozenplague.net](http://www.frozenplague.net)  
> Feel free to add me to MSN and/or GTalk as this email.

Thanks for that that plugin that brings me a step closer.  
Now i got this error.

Mysql::Error: #42S02Table ‘webdesign\_development.sidebars’ doesn’t  
exist: SELECT \* FROM `sidebars` WHERE (sidebars.admin\_id = 1) LIMIT 1

I never worked with acts\_as\_list so i don’t know what i must set into  
that table a able with only admin\_id gives no response

---

<div class="post-metadata">

**Author:** ![gorter9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gorter9](https://www.ruby-forum.com/u/gorter9)\
**Post date:** [January 11, 2008, 4:31pm UTC](https://www.ruby-forum.com/t/order-by-array/125466/9 "2008-01-11T16:31:38Z")

</div>

On Jan 11, 2008, at 9:09 AM, GA Gorter wrote:

> > 
> 
> LIMIT 1
> 
> I never worked with acts\_as\_list so i don’t know what i must set into  
> that table a able with only admin\_id gives no response

It’s looking for a table called ‘sidebars’ in your  
webdesign\_development schema. I have to guess (because you haven’t  
supplied any additional code) that you’re trying my suggestion of a  
Sidebar model. You also have to create that table (with a database  
migration) and then get the values in there.

If you want us to be able to help you, give more context to your posts  
and include actual code. Also, you can certainly try searching Google  
for things, too. (Hint: googling for “acts\_as\_list” would have told  
you exactly what Ryan did.)

-Rob

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