按集合中的列对多个条目进行分组

时间:2022-03-15 15:36:00

I have a collection of records I am showing in a table within a view file that looks as follows:

我在视图文件的表中显示的记录集合如下所示:

|| user || sport || # of victories || # of losses ||  
|| Jane || foo   ||       1        ||      1      ||  
|| Jane || foo   ||       2        ||      3      ||  
|| Jane || bar   ||       1        ||      1      ||  
|| Jane || bar   ||       2        ||      3      ||

In the example above, I am trying to group the results based on the sport, so rather than having the 2 records

在上面的例子中,我试图根据运动对结果进行分组,而不是拥有2条记录

|| Jane || foo   ||       1        ||      1      ||  
|| Jane || foo   ||       2        ||      3      || 

There would be a single grouping, which is currently broken up into multiple entries based on another column I'm not displaying on this view.

将会有一个分组,目前根据我未在此视图上显示的另一列分为多个条目。

|| Jane || foo   ||       3        ||      4      ||  

I've been trying .group and .group_by to accomplish this, but don't seem to be doing this correctly. Can someone please provide some pointers?

我一直在尝试.group和.group_by来完成这个,但似乎没有正确地做到这一点。有人可以提供一些指示吗?

Additional info, if needed:

如果需要,还有其他信息:

Table schema

表模式

create_table "records", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
 t.integer "victories"
 t.integer "losses"
 t.bigint "user_id"
 t.bigint "sport_id"
 t.datetime "created_at", null: false
 t.datetime "updated_at", null: false
 t.integer "opposition_id"
 t.index ["opposition_id"], name: "index_records_on_category_id"
 t.index ["user_id"], name: "index_records_on_user_id"
end

Controller

调节器

  def board
    @summary = Record.all.sort_by{ |i| i.user_id }
  end

View

视图

   <% @summary.each do |record| %>
     <tr>
      <td><%= record.user.name %></td>
      <td><%= record.sport_id %></td>
      <td><%= record.victories %></td>
      <td><%= record.losses %> </td>
    </tr>
   <% end %>

1 个解决方案

#1


1  

Or use a query, just as example I placed all in the view, but use the controller to fetch data.

或者使用查询,就像我将所有内容放在视图中一样,但使用控制器来获取数据。

<% sport_records = SportRecord.group(:user_id).select(:user_id, :sport_id, "sum(victories) as total_victories", "sum(losses) as total_losses") %>
<% sport_records.each do |sr| %>
  <p><%= sr.sport.name %> | <%= sr.user.name %> | <%= sr.total_victories %> | <%= sr.total_losses %></p>
<% end %>

If you also need to group by sport, change into

如果你还需要按运动分组,请换成

SportRecord.group(:user_id, :sport_id).select(:user_id, :sport_id, "sum(victories) as total_victories", "sum(losses) as total_losses")

#1


1  

Or use a query, just as example I placed all in the view, but use the controller to fetch data.

或者使用查询,就像我将所有内容放在视图中一样,但使用控制器来获取数据。

<% sport_records = SportRecord.group(:user_id).select(:user_id, :sport_id, "sum(victories) as total_victories", "sum(losses) as total_losses") %>
<% sport_records.each do |sr| %>
  <p><%= sr.sport.name %> | <%= sr.user.name %> | <%= sr.total_victories %> | <%= sr.total_losses %></p>
<% end %>

If you also need to group by sport, change into

如果你还需要按运动分组,请换成

SportRecord.group(:user_id, :sport_id).select(:user_id, :sport_id, "sum(victories) as total_victories", "sum(losses) as total_losses")