This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 5 years have passed since last update.

大学生管理アプリの作成11(いくつかの点を修正して、良いアプリにしよう)

Last updated at Posted at 2019-05-24

学科の詳細画面に、「所属している生徒」を表示します

関連性を追加

app/models/subject.rbを編集

app/models/subject.rb
class Subject < ApplicationRecord
  has_many :students
end

学部の詳細ページに生徒情報の詳細を追加

app/views/subjects/show.html.erbを変更します。

app/views/subjects/show.html.erb
<p>
  <strong>学部名:</strong>                                                                                                                                       
  <%= @subject.name %>
</p>
<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Subject</th>
      <th>Email</th>
      <th>Gender</th>
      <th>Age</th>
      <th>Opinion</th>
    </tr>
  </thead>

  <tbody>
    <% @subject.students.each do |student| %>
      <tr>
        <td><%= student.name %></td>
        <td><%= student.subject.name %></td>
        <td><%= student.email %></td>
        <td><%= student.gender %></td>
        <td><%= student.age %></td>
        <td><%= student.opinion %></td>
      </tr>
    <% end %>
  </tbody>
</table>

スクリーンショット 2019-05-24 13.51.08.png

サークルの詳細画面に、「所属している生徒」を表示します

関連性を追加

app/models/club.rbを編集

app/models/club.rb
class Club < ApplicationRecord
  has_many :club_students
  has_many :students, through: :club_students
end

app/views/clubs/show.html.erbを編集

app/views/clubs/show.html.erb
<p id="notice"><%= notice %></p>

<p>
  <strong>Name:</strong>
  <%= @club.name %>
</p>
<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Subject</th>
      <th>Email</th>
      <th>Gender</th>
      <th>Age</th>
      <th>Opinion</th>
    </tr>
  </thead>

  <tbody>
    <% @club.students.each do |student| %>
      <tr>
        <td><%= student.name %></td>
        <td><%= student.subject.name %></td>
        <td><%= student.email %></td>
        <td><%= student.gender %></td>
        <td><%= student.age %></td>
        <td><%= student.opinion %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<%= link_to 'Edit', edit_club_path(@club) %> |
<%= link_to 'Back', clubs_path %>

スクリーンショット 2019-05-24 16.09.47.png

サークルの一覧画面に、「所属している生徒の数」を表示します

app/models/club.rbを編集

app/models/club.rb
class Club < ApplicationRecord                                                                                                                                         
  has_many :club_students
  has_many :students, through: :club_students
end
app/controllers/clubs_controller.rb
def index
    @clubs = Club.all
    students_count = Club.joins(:students).group(:id).select(:id).select('COUNT(students.id) as cnt')
    @student_h = {}
    students_count.each do |s_c|
      @student_h[s_c.id] = s_c.cnt
    end
  end
app/views/clubs/index.html.erb
<tbody>
    <% @clubs.each do |club| %>
      <tr>
        <td><%= club.name %></td>
        <td><%= @student_h[club.id] %></td>
        <td><%= link_to 'Show', club %></td>
        <td><%= link_to 'Edit', edit_club_path(club) %></td>
        <td><%= link_to 'Destroy', club, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>

スクリーンショット 2019-05-27 13.17.36.png

生徒の詳細画面に、「所属しているサークル情報」と「テスト結果」を表示します

clubに関する関連性を追加

app/models/student.rb
class Student < ApplicationRecord
  has_many :club_students
  has_many :clubs, through: :club_students
  belongs_to :subject                                                                                                                                                  

  enum gender: { male: 0, female: 1 }
end

変更箇所1
app/views/students/show.html.erbを変更

app/views/students/show.html.erb
<p>
  <strong>Subject:</strong>
  <%= @student.subject.name %>                                                                                                                                         
</p>

変更箇所2
app/views/students/show.html.erbを変更
以下の箇所を追記

app/views/students/show.html.erb
<h1>サークル情報</h1>
<table>
  <thead>
    <tr>
      <th>Name</th>
    </tr>
  </thead>

  <tbody>
    <% @student.clubs.each do |club| %>
      <tr>
        <td><%= club.name %></td>
      </tr>
    <% end %>
  </tbody>
</table>

スクリーンショット 2019-05-24 16.46.11.png

exam_resultsに関する関連性を追加

app/models/student.rb
class Student < ApplicationRecord
  has_many :club_students
  has_many :clubs, through: :club_students
  has_many :exam_results
  belongs_to :subject                                                                                                                                                  

  enum gender: { male: 0, female: 1 }
end

app/views/students/show.html.erbを変更
以下の箇所を追記

app/views/students/show.html.erb
<h1>試験結果情報</h1>
<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Score</th>
    </tr>
  </thead>

  <tbody>
    <% @student.exam_results.each do |exam_result| %>
      <tr>
        <td><%= exam_result.name %></td>
        <td><%= exam_result.score %></td>                                                                                                                        
      </tr>
    <% end %>
  </tbody>
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up