0
@users = User.find_by_username(params[:username]).all_following.order("created_at DESC").paginate(page: params[:page])

This calculation won't work:(
I'd like to use pagination and order sort with acts_as_follower.

HUSTEN
  • 5,117
  • 4
  • 24
  • 38

2 Answers2

0

Updated answer:

@users = User.find_by_username(params[:username]).all_following.sort_by{|i| i.created_at}
Kaminari.paginate_array(@users).page(params[:page]).per(10)

OR

Mix with Ahmad's answer

@users = User.find_by_username(params[:username]).all_following(order: 'created_at DESC')
Kaminari.paginate_array(@users).page(params[:page]).per(10)

ref: acts_as_follower github

ref: kaminari github

emrahbasman
  • 2,003
  • 10
  • 19
  • I tried your code but it says users_controller.rb:66: syntax error, unexpected ')', expecting '}' ...owing.sort_by{|i| i.created_at).paginate(page: params[:page]) – HUSTEN Dec 18 '12 at 23:50
  • there was a typo in sort_by. Updated the answer. – emrahbasman Dec 19 '12 at 08:41
  • Thanks but it says syntax error, unexpected tIVAR, expecting keyword_end ...e: params[:page]) – HUSTEN Dec 19 '12 at 08:44
  • Thanks but it still says undefined method `current_page' for #..... Is there something wrong with my view? I coded like this <%= paginate @users %> – HUSTEN Dec 19 '12 at 09:51
0

You can pass the order (and any other option ActiveRecord#all accepts) directly to all_following like this

@users = User.find_by_username(params[:username]).all_following(:order => 'created_at DESC').paginate(page: params[:page])

UPDATE: will_paginate can paginate an array but you have to tell Rails to include this part as it's not included by default, please check this answer.

Community
  • 1
  • 1
Ahmad Sherif
  • 5,923
  • 3
  • 21
  • 27