0
votes

I have the following models:

  • Institute
  • Student
  • Classroom
  • Course

Requirements:

  1. A classroom can have many courses. A course can belong to many classrooms.
  2. A classroom can have many students. A student can belong to many classrooms.
  3. A student can take many courses. A course can belong to many students.
  4. An institute has many students, many classrooms and many courses.

I'm trying to create associations for the above: Would the following be accurate?

  1. Institute: has_and_belongs_to_many :students | has_many :classrooms | has_many :courses
  2. Student: has_and_belongs_to_many :institutes | has_and_belongs_to_many :classrooms | has_many :courses | belongs_to :institute
  3. Classroom: has_and_belongs_to_many :students | has_many :courses | belongs_to :institute
  4. Course: has_and_belongs_to_many :students | has_many :classrooms | belongs_to :institute

How do I use the "through" relationship here?

1
Please refer Active Record Associations on Rails Guides - Lahiru Jayaratne

1 Answers

0
votes

Do the similar for Classroom associations and Cource association

run:

ruby homework.rb

#homework.rb
require 'active_record'

`rm foobar.db`

class User < ActiveRecord::Base
  establish_connection adapter: 'sqlite3', database: 'foobar.db'
  connection.create_table table_name, force: true do |t|
    t.string :name
  end
  connection.create_table :institutes_users, id: false do |t|
    t.integer :user_id
    t.integer :institute_id
  end

  has_and_belongs_to_many :institutes
end

class Institute < ActiveRecord::Base
  establish_connection adapter: 'sqlite3', database: 'foobar.db'
  connection.create_table table_name, force: true do |t|
    t.string :name
  end
  connection.create_table :institutes_students, id: false do |t|
    t.integer :student_id
    t.integer :institute_id
  end

  has_and_belongs_to_many :users
  has_and_belongs_to_many :students
  has_many :classrooms
  has_many :courses
end

class Student < ActiveRecord::Base
  establish_connection adapter: 'sqlite3', database: 'foobar.db'
  connection.create_table table_name, force: true do |t|
    t.string :name
  end
  connection.create_table :classrooms_students, id: false do |t|
    t.integer :student_id
    t.integer :classroom_id
  end

  has_and_belongs_to_many :institutes
  has_and_belongs_to_many :classrooms
  has_many :courses
end

class Classroom < ActiveRecord::Base
  establish_connection adapter: 'sqlite3', database: 'foobar.db'
  connection.create_table table_name, force: true do |t|
    t.string :name
    t.references :institute
  end

  belongs_to :institute
  has_and_belongs_to_many :students
end

class Course < ActiveRecord::Base
  establish_connection adapter: 'sqlite3', database: 'foobar.db'
  connection.create_table table_name, force: true do |t|
    t.string :name
    t.references :institute
    t.references :student
  end

  belongs_to :institute
  belongs_to :student
end
user = User.create!(name: 'John')
institute = Institute.create!(name: 'Harvard')
student = Student.create(name: 'Mary')
institute.users << user
institute.classrooms.create(name: '4th floor room')
institute.courses.create(name: 'IT')
institute.students << student
student.classrooms << Classroom.create(name: '5th froor room')
student.courses << Course.create(name: 'Biology')
p institute.users
p institute.classrooms
p institute.courses
p institute.students
p student.classrooms
p student.courses