2013-06-12 21 views
1

我使用所谓的acts_as_followerhttps://github.com/tcocca/acts_as_follower)宝石为什么在尝试排序时返回没有方法错误?

在这里,我试图获取所有谁是下CURRENT_USER用户。

然后我希望他们通过所谓的列last_active_at

于是,我像这样的代码排序,但它返回的错误。 我该如何解决?

控制器

@followed_users = current_user.followers.order('users.last_active_at DESC').limit(10) 

错误消息

NoMethodError (undefined method `order' for #<Array:0x0000000ab3cf18>): 
+0

请更新与型号代码的问题。 –

+0

@ManojMonga或者你能告诉我如何获取DESC订购的记录吗?与此代码'@followed_users = current_user.followers.sort_by {| n | (10)' – MKK

+1

请参阅''current_user.followers''是什么在这里获取数组中的追随者。根据对我的回答的评论,它给出了错误。无论如何,如果这是为你工作,仍然无法调整它只获取10条记录,因为这个查询正在由宝石处理。 –

回答

1

,所以你可以试试这个

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)

2

followers方法返回ArrayArray没有在任何红宝石方法order

请看看 从github: -

“book.followers#返回所有的追随者那本书,不同的对象类型的集合(如数组。类型用户或类型的书)”

+0

感谢您的信息。有没有解决这个问题的方法? – MKK

+0

请看我的意见 –

+0

谢谢我做到了!你的评论工作,但还有一件事!你能否改变为DESC而不是ASC? – MKK

1

阵列不要有任何order方法。你可以做这样的事情

@followed_users = current_user.followers.sort_by{ |n| n.last_active_at } 

然后在视图中显示时,您可以在限制你要多少曾经或做从控制器(建议方式)

+0

谢谢:)但限制(10)? – MKK

+2

@followed_users = current_user.followers.sort_by {| n | n.last_active_at} .take(10)谢谢 –

+0

谢谢!但它是ASC。你可以改变它为DESC? – MKK

0

按照[email protected]追随者采取的ActiveRecord的选项可选的哈希参数(:极限:秩序,等...)

所以这应该工作:

@followed_users = current_user.followers(:order => 'users.last_active_at DESC', :limit => 10) 
+0

它返回了这个错误'ActiveRecord :: EagerLoadPolymorphicError(不能急切地加载多态关联:跟随者):' – MKK

+0

它只是在'current_user.followers'上工作吗? –

+0

不,它不起作用 – MKK

相关问题