Tara Roskell
Tara Roskell

Reputation: 11

Understanding Rails Associations - Which way round

I am a Rails newbie and struggling to understand Associations. I have read the rails guides and understand a basic relations ship between two models but am struggling to understand how to work things out with 3.

For example - Say I wanted to create some sort of marking/grading system where a teacher could set their own criteria for marking students - eg. 1. comprehension, 2. completeness, 3. neatness etc and then each of these would have a score which could create an average.

I am assuming I would need 3 models - Student, Criteria (ie grading criteria) and then Score?

How would each of these models relate to each other? I have been trying different things but they don't seem to be working.

Thanks in advance for any help understanding.

Upvotes: 1

Views: 114

Answers (1)

Jesse Wolgamott
Jesse Wolgamott

Reputation: 40277

Teacher
  has_many :students
end

Student
  has_many :scores
  belongs_to :teacher
end

Criteria
  has_many :scores
  has_many :students, through: :scores
end

Score
  has_many :students
  has_many :criteria
end

So, the student has many scores (which will have the criteria_id, the student_id, and the actual score the teacher records.

Upvotes: 1

Related Questions