[英]Rails 4 Error: NoMethodError - undefined method
我正在努力通過開發者對tools
的支持來通過我的“開發用戶”訂購循環。
在該循環中,我得到了要用於排序元素的值:
user.tools.map(&:cached_votes_up).sum
那是我的循環:
- @users.order(user.tools.map(&:cached_votes_up).sum).each do |user| #incorrect!
而我的控制器:
@users = User.all
如果我的循環中沒有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>
感謝您的幫助!
您可以使用joins
@users = User.joins(:tools).group("users.id").order("SUM(tools.cached_votes_up)")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.