如何对连接表进行排序?

时间:2015-06-25 10:49:03

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

帮助请写orm-request。

表:

users:
id: integer
name: varchar

posts:
id: integer
title: varchar
user_id: integer
views: integer

模型:

User:
class User < ActiveRecord::Base
  has_many    :posts,  dependent:  :destroy
end

Posts:
class Post < ActiveRecord::Base
  belongs_to  :user
end

控制器:

def popular_diary
  @users = User.joins(:posts).group_by(:user_id).order('SUM(posts.views)')
end

HTML:

  <% @users.each do |user| %>
    <div class="row">
      <%= link_to user_posts_url(user.id) do %>
        <div><%= user.name %></div>
      <% end %>  
    </div>
  <% end %>

我需要所有用户按特定顺序孵化。 order ('SUM (posts.views)

问题是屏幕显示以下错误消息:

  

错误的参数数量(0表示1)

1 个答案:

答案 0 :(得分:1)

它是group,而不是group_by

def popular_diary
  @users = User.joins(:posts).group(:user_id).order('SUM(posts.views)')
end