user211662
user211662

Reputation: 18427

Using :order and :limit options - Ruby On Rails

Is there any way I can use the :limit and :order options in the find method. I'm trying to sort activities by descending order, such that the newest activities are shown. However, when I try to use the (:all, :limit => 5, :order=> 'Date desc) I get an error. I need to limit to only 5 records, and when I disregard the order option, it works but not what I need...

Thanks

Upvotes: 3

Views: 13569

Answers (2)

Michael Sepcot
Michael Sepcot

Reputation: 11385

Is your date column actually called date? First, I would change that, date is the name of a function in most databases, that could be the cause of the error you are seeing. Rails uses created_at, updated_at, etc, so following that naming scheme will make your code more readable to future maintainers.

You could try to quote the column name in back-ticks:

:order => "`date` desc"

Upvotes: 0

Kieran Hayes
Kieran Hayes

Reputation: 641

I think you missed a quote in your example.

Model.find(:all, :limit => 5, :order=> 'created_at desc')

Make sure that a date column exists in your table.

Upvotes: 9

Related Questions