Jack
Jack

Reputation: 3878

Checking a related relationship once the instance is created

I have a schema where:

Students

Courses

Assignments

Grades

I would like to add functionality whereby if a grade is added and the student does not belong to the course that the grade's assignment belongs to, then this relationship is made. Any suggestions as to the best way to do this? The grades_courses table does not have it's own model, will this need to be made?

A friend has suggested using after_create, but I don't know how to pass the parameters to this.

Upvotes: 1

Views: 48

Answers (1)

Gordon Isnor
Gordon Isnor

Reputation: 2105

How about an observer on grades? Something like this

class GradeObserver < ActiveRecord::Observer

  def after_create(grade)
    unless grade.assignment.course.students.include?(grade.student)
      grade.assignment.course.students << grade.student 
    end
  end

end

Upvotes: 2

Related Questions