Ruby on rails: correct solution for many-to-many associations?

Viewed 21

I'm new to ruby on rails and am trying to set up student grading system.

Each student has many courses. Each course has many units. Each students needs a grade for each unit.

Models

class Student < ApplicationRecord
validates :student_id, :name, :group, presence: true
has_many :student_classes
has_many :courses, through: :student_classes
has_many :units, through: :courses
has_many :units, through: :grades
has_many :grades
end

class StudentClass < ApplicationRecord
belongs_to :student
belongs_to :course
end

class Course < ApplicationRecord
has_many :student_classes
has_many :students, through: :student_classes
has_many :units
end

class Unit < ApplicationRecord
belongs_to :course
has_many :students, through: :grades
has_many :grades
end

class Grade < ApplicationRecord
belongs_to :student
belongs_to :unit
end

Migration

class CreateStudents < ActiveRecord::Migration[7.0]
def change
create_table :students do |t|
  t.integer :uid
  t.string :group
  t.integer :student_id
  t.string :name
  t.string :first_name
  t.string :middle_name
  t.string :last_name
  t.integer :phone
  t.string :home_school
  t.string :external_email
  t.string :usi
  t.datetime :dob
  t.boolean :lnn_assessment_registered_status
  t.boolean :lnn_assessment_completed_status
  t.boolean :enrolment_paperwork_completed__status
  t.datetime :enrolment_paperwork_completed_date
  t.string :enrolled_in_energySpace_userid
  t.boolean :teams_group_and_chat_created_and_user_added_status
  t.string :Comments
  t.datetime :date
  t.string :assessor_name
  t.string :campus
  t.boolean :yes_no_status
  t.string :satisfactory
  t.string :competency
  t.string :course_code
  t.string :course_name

  t.timestamps
end



class CreateStudentClasses < ActiveRecord::Migration[7.0]
def change
create_table :student_classes do |t|
  t.references :student, null: false, foreign_key: true
  t.references :course, null: false, foreign_key: true

  t.timestamps
end


class CreateCourses < ActiveRecord::Migration[7.0]
def change
create_table :courses do |t|
  t.string :course_name
  t.string :course_code

  t.timestamps
end

class CreateGrades < ActiveRecord::Migration[7.0]
def change
create_table :grades do |t|
  t.references :student, null: false, foreign_key: true
  t.references :unit, null: false, foreign_key: true
  t.boolean :enrolled
  t.boolean :cy
  t.boolean :awaiting_confirmation
  t.boolean :profiling
  t.boolean :missed
  t.boolean :skills
  t.boolean :ukt
  t.boolean :waiting_on_staff

  t.timestamps
end



class CreateUnits < ActiveRecord::Migration[7.0]
def change
create_table :units do |t|
  t.string :unit_name
  t.string :unit_code
  t.integer :course_id

  t.timestamps
end

I am able to view the code with in the course model with

<h1><%= @course.course_name %></h1>


<table>
<tr >

<tr class="student-list-row">
  <th>Unit Name</th>
  <th>Unit Code</th>
  <th>Unit ID</th>

  <% @course.units.each do |unit| %>

    <tr class="student-list-row">
      <td class="student-list-item"><%= unit.unit_name %></td>
      <td class="student-list-item"><%= unit.unit_code %></td>
      <td class="student-list-item"><%= unit.id %></td>

      <td><%= link_to 'Show' %></td>
    </tr>
  <% end %>
</tr>
</tr> 

But I would like to create a table that has all units in a course as the columns, and the students as the rows, with each grade being shown. Is this possible to do with this arrangement?

Is this also able to be achieved whilst viewing all courses/units a student is assigned to?

0 Answers
Related