【问题标题】:Rails 4 Error: NoMethodError - undefined methodRails 4 错误:NoMethodError - 未定义的方法
【发布时间】:2016-09-18 10:38:51
【问题描述】:

我正在努力通过我的设计用户通过他们创建的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>

感谢每一个帮助!

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 devise


    【解决方案1】:

    你可以使用joins

    @users = User.joins(:tools).group("users.id").order("SUM(tools.cached_votes_up)")
    

    【讨论】:

    • 完美运行,谢谢!我会尽快接受你的回答:)
    猜你喜欢
    • 2017-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多