用于对具有相同类别的所有团队进行分组的SQL查询

时间:2016-11-08 21:11:15

标签: ruby-on-rails postgresql activerecord

我有一个应用程序,用户可以使用团队和积分创建锦标赛表。 我有冠军桌。将保存所有球队,积分,比赛,球门差异等。

所以这是我的模特:

class Championship < ActiveRecord::Base
  belongs_to :category

  validates :team, presence: true
end

class Category < ActiveRecord::Base
  has_many :events
  has_many :championships

  validates :name, presence: true
end

使用模型类别,用户可以创建年龄类别(初学者,青少年,老年人)。

schema.rb中的表

 create_table "categories", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "championships", force: :cascade do |t|
    t.string   "team"
    t.integer  "games_played",    default: 0
    t.integer  "wins",            default: 0
    t.integer  "draws",           default: 0
    t.integer  "lost",            default: 0
    t.string   "goal_difference", default: "0 - 0"
    t.integer  "points",          default: 0
    t.datetime "created_at",                        null: false
    t.datetime "updated_at",                        null: false
    t.integer  "category_id"
  end

查看:

<tbody>
        <% @championships.each do |championship| %>
          <tr>
            <td><%= championship.team %></td>
            <td><%= best_in_place [:admin, championship], :games_played, as: :input %></td>
            <td><%= best_in_place [:admin, championship], :wins, as: :input %></td>
            <td><%= best_in_place [:admin, championship], :draws, as: :input %></td>
            <td><%= best_in_place [:admin, championship], :lost, as: :input %></td>
            <td><%= best_in_place [:admin, championship], :goal_difference, as: :input %></td>
            <td><%= best_in_place [:admin, championship], :points, as: :input %></td>
            <td><%= link_to edit_admin_championship_path(championship) do %>
              <i class="material-icons edit-event">mode edit</i>
            <% end %>
            </td>
            <td><%= link_to admin_championship_path(championship), method: :delete, data: { confirm: "Jeste li sigurni?" }, class: "delete" do %>
              <i class="material-icons">delete</i>
            <% end %></td>
          </tr>
        <% end %>
      </tbody>

我想选择所有具有相同类别的球队,并将它们放在一个冠军赛表中。我希望按类别选择所有队伍,以便所有小辈都在一张桌子里,第二张桌子的初学者和第三张桌子的老年人。

问题:如何编写SQL查询以选择所有具有相同类别的团队?

1 个答案:

答案 0 :(得分:1)

您可以使用group_by http://apidock.com/rails/Enumerable/group_by。它看起来像这样

Championship.all.group_by{|champ| champ.category.name}.each do |category, champs| 
      puts category.to_s
      champs.each do |champ|
        puts champ.team
      end
    end

在视图中

<% Championship.all.group_by{|champ| champ.category.name}.each do |category, champs|  %>
      <tr>
        <td><%= category %></td>
      </tr>
    <% champs.each do |championship| %>
      <tr>
        <td><%= championship.team %></td>
        <td><%= best_in_place [:admin, championship], :games_played, as: :input %></td>
        <td><%= best_in_place [:admin, championship], :wins, as: :input %></td>
        <td><%= best_in_place [:admin, championship], :draws, as: :input %></td>
        <td><%= best_in_place [:admin, championship], :lost, as: :input %></td>
        <td><%= best_in_place [:admin, championship], :goal_difference, as: :input %></td>
        <td><%= best_in_place [:admin, championship], :points, as: :input %></td>
        <td><%= link_to edit_admin_championship_path(championship) do %>
          <i class="material-icons edit-event">mode edit</i>
        <% end %>
        </td>
        <td><%= link_to admin_championship_path(championship), method: :delete, data: { confirm: "Jeste li sigurni?" }, class: "delete" do %>
          <i class="material-icons">delete</i>
        <% end %></td>
      </tr>
    <% end %>
<% end %>