Reputation: 2753
I'm using the gem called acts_as_follower
(https://github.com/tcocca/acts_as_follower)
Here, I'm trying to fetch all the users who are following current_user.
then I want them ordered by the column called last_active_at
So I tried to code like this but it returned error. How can I fix?
controller
@followed_users = current_user.followers.order('users.last_active_at DESC').limit(10)
Error Message
NoMethodError (undefined method `order' for #<Array:0x0000000ab3cf18>):
Upvotes: 1
Views: 148
Reputation: 12330
so you can try this
ASC:
current_user.followers.sort!{ |a,b| a.created_at <=> b.created_at }.take(10)
DESC:
current_user.followers.sort!{ |a,b| b.created_at <=> a.created_at }.take(10)
Upvotes: 1
Reputation: 910
Arrays dont have any order
method. You could do something like this
@followed_users = current_user.followers.sort_by{ |n| n.last_active_at }
Then when displaying in the view you can limit it to how many ever you want or do it from the controller (suggested way).
Upvotes: 1
Reputation: 12330
followers
method return Array
and Array
do not have any order
method in ruby
Please look From github:--
"book.followers # Returns an array of all the followers for that book, a collection of different object types (eg. type User or type Book)"
Upvotes: 2
Reputation: 3083
According to the readme@github followers takes an optional hash parameter of ActiveRecord options (:limit, :order, etc…)
So this should work:
@followed_users = current_user.followers(:order => 'users.last_active_at DESC', :limit => 10)
Upvotes: 0