Find_all order question

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.

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 http://www.manning.com/black!

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!

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/ , the
basics
are:

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

The default is ascending.

hth,
Bill

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?

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

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

/ CJ

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?

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

-Jonathan.

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