striff88
striff88

Reputation: 97

How do I model this relationship for each unique user?

I am curious how I would go about implementing this.

I am creating an online learning website. There are a few courses on this website that users can complete. Courses have complete attribute which is just a boolean.

I want each user's progress to be trackable. So let's say I am at the course show page and I want to be able to do

@course.complete? 

and get a unique response for each user.

Right now I have a user model set up which can log in and out, but I do not have a relationship between users and courses.

What the best way to set up this relationship so that each course is unique to each user?

i.e. if User A has complete the course then it will show true. If User B has not completed the course then it will show false.

Thanks!

Upvotes: 0

Views: 64

Answers (1)

Ermin Dedovic
Ermin Dedovic

Reputation: 907

I would do it like this

class Student < ActiveRecord::Base
  has_many :course_enrollments
  has_many :courses, :through => :course_enrollments
  # code here
end

class Course < ActiveRecord::Base
  has_many :course_enrollments
  has_many :students, :through => :course_enrollments
  # code here
end

class CourseEnrollment < ActiveRecord::Base
  belongs_to :student
  belongs_to :course
  # code here
end

Upvotes: 1

Related Questions