# Find\_all order question

**URL:** <https://www.ruby-forum.com/t/find-all-order-question/97646>\
**Category:** Rails\
**Created:** [July 9, 2006, 3:33am UTC](https://www.ruby-forum.com/t/find-all-order-question/97646 "2006-07-09T03:33:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![thekog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@thekog](https://www.ruby-forum.com/u/thekog)\
**Post date:** [July 9, 2006, 3:33am UTC](https://www.ruby-forum.com/t/find-all-order-question/97646/1 "2006-07-09T03:33:08Z")

</div>

How does one properly get find\_all to grab the data from the DB table  
ordered by a specific column?

For example:

@anglers = Angler.find\_all

This code gets all the angler records and they are sorted in order of  
the id field which is of course set as a primary key in the DB (mysql).

I’d really like to pull the data from the DB sorted in the order of  
another field, for example ‘score’ so I attempted to code:

@anglers = Angler.find\_all nil, ‘score’  
OR  
@anglers = Angler.find\_all(nil, ‘score’)

Neither worked and the data continues to be ordered by id. Do I need to  
go and make the ‘score’ coumn a secondary key in the DB for this to  
work? Or should RoR be doing this on its own? Or is there something else  
I’m missing.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![thekog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@thekog](https://www.ruby-forum.com/u/thekog)\
**Post date:** [July 9, 2006, 3:38am UTC](https://www.ruby-forum.com/t/find-all-order-question/97646/2 "2006-07-09T03:38:51Z")

</div>

Hi –

On Sun, 9 Jul 2006, Mike K. wrote:

> I’d really like to pull the data from the DB sorted in the order of  
> another field, for example ‘score’ so I attempted to code:
> 
> @anglers = Angler.find\_all nil, ‘score’  
> OR  
> @anglers = Angler.find\_all(nil, ‘score’)
> 
> Neither worked and the data continues to be ordered by id. Do I need to  
> go and make the ‘score’ coumn a secondary key in the DB for this to  
> work? Or should RoR be doing this on its own? Or is there something else  
> I’m missing.

find\_all is actually deprecated in favor of find(:all), to which you  
can append a SQL fragment for the order, like this:

Angler.find(:all, :order =\> “score ASC”)

David

–  
“To fully realize the potential of Rails, it’s crucial that you take  
the time to fully understand Ruby–and with “Ruby for Rails” David  
has provided just what you need to help you achieve that goal.”  
– DAVID HEINEMEIER HANSSON, in the foreword to RUBY FOR RAILS.  
Complete foreword & sample chapters at [Ruby for Rails](http://www.manning.com/black)!

---

<div class="post-metadata">

**Author:** ![thekog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@thekog](https://www.ruby-forum.com/u/thekog)\
**Post date:** [July 9, 2006, 3:49am UTC](https://www.ruby-forum.com/t/find-all-order-question/97646/3 "2006-07-09T03:49:14Z")

</div>

Thanks David I tried that and it is still dragging the data out in order  
of id not the score field (which is int(5)). I am on MySQL 4.X.  
Dadgumit!

---

<div class="post-metadata">

**Author:** ![thekog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@thekog](https://www.ruby-forum.com/u/thekog)\
**Post date:** [July 9, 2006, 3:51am UTC](https://www.ruby-forum.com/t/find-all-order-question/97646/4 "2006-07-09T03:51:58Z")

</div>

Hi Mike,

Mike K. wrote:

> How does one properly get find\_all to grab the data from the DB table  
> ordered by a specific column?
> 
> For example:
> 
> @anglers = Angler.find\_all

According to the documentation at [http://api.rubyonrails.org/](http://api.rubyonrails.org/) , the  
basics  
are:

@anglers = Angler.find(:all, :order =\> “column\_name DESC”)

The default is ascending.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![thekog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@thekog](https://www.ruby-forum.com/u/thekog)\
**Post date:** [July 9, 2006, 4:04am UTC](https://www.ruby-forum.com/t/find-all-order-question/97646/5 "2006-07-09T04:04:03Z")

</div>

Thanks guys I had another statement that was hosing things in the method  
I did not show. I am now getting sorted ascending – of course, I need  
descending. Is it easy as changing the SQL query?

---

<div class="post-metadata">

**Author:** ![thekog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@thekog](https://www.ruby-forum.com/u/thekog)\
**Post date:** [July 9, 2006, 4:05am UTC](https://www.ruby-forum.com/t/find-all-order-question/97646/6 "2006-07-09T04:05:11Z")

</div>

Never mind - DESC! LOL this is getting better every night. Thank you for  
your guidance 😉

---

<div class="post-metadata">

**Author:** ![thekog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@thekog](https://www.ruby-forum.com/u/thekog)\
**Post date:** [July 9, 2006, 8:18am UTC](https://www.ruby-forum.com/t/find-all-order-question/97646/7 "2006-07-09T08:18:02Z")

</div>

Mike, you might want to take a look at the documentation at  
[http://api.rubyonrails.org](http://api.rubyonrails.org) for questions like these. It’s really quite  
good.

/ CJ

---

<div class="post-metadata">

**Author:** ![thekog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@thekog](https://www.ruby-forum.com/u/thekog)\
**Post date:** [July 9, 2006, 3:13pm UTC](https://www.ruby-forum.com/t/find-all-order-question/97646/8 "2006-07-09T15:13:56Z")

</div>

CJ I have been using the API, although I find it’s syntax difficult to  
decipher at times – probably just the learning curve of how Ruby is  
specified.

What is the reason for the different syntax styles, aka Entry.find\_all  
vs Entry.find(:all)?

Why the 2 different styles and where do they come from?

---

<div class="post-metadata">

**Author:** ![thekog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3da27b/32.png) [@thekog](https://www.ruby-forum.com/u/thekog)\
**Post date:** [July 10, 2006, 4:13am UTC](https://www.ruby-forum.com/t/find-all-order-question/97646/9 "2006-07-10T04:13:54Z")

</div>

As far as I know, find\_all is an old api that has been deprecated,  
find(:all) is the recommended way.

-Jonathan.
