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.
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 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?