Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select everything in a database WHERE & order by

I'm doing the following query...

Status.where(:status => params[:cat_id])

but I want to order the results by the created_at column. I've tried everything logical like

Status.where(:status => params[:cat_id]), :order => "created_at DESC"

and

Status.where(:status => params[:cat_id], :order => "created_at DESC")

but nothing seems to work.

What's the best way to order results that you're getting from a where query?

Any help is much appreciated, thanks!

like image 571
Jon Avatar asked Aug 29 '11 22:08

Jon


2 Answers

Status.where(:status => params[:cat_id]).order("created_at DESC")

You might want to give a look at the ActiveRecord API documentation.

like image 172
Simone Carletti Avatar answered Oct 02 '22 21:10

Simone Carletti


You can do using symbol:

.order(created_at: :DESC)
like image 22
Darlan D. Avatar answered Oct 02 '22 21:10

Darlan D.