I'm using the rails gem kaminari (https://github.com/amatsuda/kaminari) in order to paginate my posts database. Currently I have the code @posts = Post.order('id').page(params[:page]).per(5)
in my controller, but this orders the pages from earliest to most recent. How do I reverse this and order from most recent to earliest?
Asked
Active
Viewed 2,829 times
3

blahdiblah
- 33,069
- 21
- 98
- 152

Vasseurth
- 6,354
- 12
- 53
- 81
2 Answers
6
in your model you can do:
default_scope order("created_at DESC")
or
default_scope order("created_at ASC")

stephenmurdoch
- 34,024
- 29
- 114
- 189
-
I'd shorten the controller code to: `@posts = Post.page(params[:page]).per(5)` – stephenmurdoch Jul 24 '11 at 21:54
-
2could i do, instead, `@posts = Post.order('created_at DESC').page(params[:page]).per(5)` ? – Vasseurth Jul 27 '11 at 01:53
-
1Post.order('created_at DESC').page... is not working for me. It is sorting ascending. – n8gard Jan 07 '13 at 21:33
1
def index
@all = Model.all
@all = Model.order(sort_column + " " + sort_direction).paginate(:per_page => 5, :page => params[:page])
end
def sort_column
Model.column_names.include?(params[:sort]) ? params[:sort] : "updated_at"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "DESC/ASC"
end

norway-firemen
- 395
- 4
- 21

user3418857
- 11
- 1