学科の詳細画面に、「所属している生徒」を表示します
関連性を追加
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>
サークルの詳細画面に、「所属している生徒」を表示します
関連性を追加
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 %>
サークルの一覧画面に、「所属している生徒の数」を表示します
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>
生徒の詳細画面に、「所属しているサークル情報」と「テスト結果」を表示します
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>
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>