[英]Rails 4 Error: NoMethodError - undefined method
I am struggling ordering my loop through my Devise Users by the upvotes on tools
created by them. 我正在努力通过开发者对
tools
的支持来通过我的“开发用户”订购循环。
Inside of that loop I get the value I would like to use to order the elments with: 在该循环中,我得到了要用于排序元素的值:
user.tools.map(&:cached_votes_up).sum
That's my loop: 那是我的循环:
- @users.order(user.tools.map(&:cached_votes_up).sum).each do |user| #incorrect!
And my controller: 而我的控制器:
@users = User.all
Without order
in my loop everything runs fine, with it this error appears: 如果我的循环中没有
order
,一切都会正常运行,出现此错误:
NoMethodError in Users#index
Showing c:/Users/Jonas/gitapps/ocubit/app/views/users/index.html.haml where line #21 raised:
undefined method `tools' for #<Class:0xac2db78>
I am grateful for each help! 感谢您的帮助!
您可以使用joins
@users = User.joins(:tools).group("users.id").order("SUM(tools.cached_votes_up)")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.