0

I have Teacher model:

class Teacher < User

  has_many :categories, dependent: :destroy
  has_many :grades, through: :categories
end

Grade model:

class Grade < ApplicationRecord

  has_many :categories, dependent: :destroy
  has_many :teachers, through: :categories

end

Category model:

class Category < ApplicationRecord

  belongs_to :teacher
  belongs_to :grade

end

Student model:

class Student < User

end

Now i want to set up relationship between Grade model and Student model (student has_one grade and a grade has_many students) with through model Category. How can i do this?

Vishal Goel
  • 170
  • 1
  • 1
  • 10
  • Yeah sort of... – Vishal Goel Apr 07 '18 at 03:30
  • I don't think it makes sense to use a single join table `Category` for associating students to grades AND teachers to grades. Either you're confused about what associations you need, or I'm confused about what real-life relationships you're trying to represent. If students are simply assigned to a grade, why not use student `belongs_to :grade` and grade `has_many :students`. Why are you trying to join _through_ the `Category` model? What is a `Category` in real life? – Nathan Apr 07 '18 at 16:38
  • Category is just a through table for many to many relationship between grades and teachers .. so I'm thinking to use same through table to set up relationship between grade and students . – Vishal Goel Apr 07 '18 at 18:47

2 Answers2

0
class Student < User 

  has_one: :grade

end

and

class Grade < ApplicationRecord

  has_many :categories, dependent: :destroy
  has_many :teachers, through: :categories
  has_many :students, through: :categories

end

and

class Category < ApplicationRecord

  belongs_to :teacher
  belongs_to :grade
  belongs_to :student

end

Does the above not work..? You should put what you have tried in your post if you have run into issues.

chevybow
  • 9,959
  • 6
  • 24
  • 39
  • Thanks.!! But doesn't need to write anything in Category model? – Vishal Goel Apr 06 '18 at 21:31
  • Whoops its the end of the work day for me so I'm drained- You probably need a belongs_to in Category too. I will edit my response now As a side-note, the shoulda-gem is really helpful in testing your associations and making sure they work as intended- I would look into it if you are having trouble verifying that you set up your models correctly. Each association can be done with one line in a spec file using the gem. I highly recommend. – chevybow Apr 06 '18 at 21:35
  • Doesn't i need to write this has_one :category has_one :grade, through: :category in student model? – Vishal Goel Apr 06 '18 at 22:02
0

Join tables are for many-to-many relationships. Since a student only has one grade, it is a many-to-one relationship, not a many-to-many relationship.

You do not need (and shouldn't use) a join table for this.

Instead add a grade_id column to the students (users) table and setup the associations like this:

class Student < User
  belongs_to :grade
end

class Grade < ApplicationRecord
  has_many :students
end

Don't use any through associations to connect students with grades.

Nathan
  • 7,816
  • 8
  • 33
  • 44