Rails associations - how can I set up associations for different varieties of users?

旧城冷巷雨未停 提交于 2019-12-06 12:18:58

问题


I'm creating a web app which consists of schools, courses, students, and teachers.

A school can have many courses and a course has one teacher and many students.

The problem I am running into is that a single user could be a teacher of one course, but a student in another course (or even a student or teacher in a course in a different school). I don't want to create a model for teachers and a separate model for students because I would like to track all my users in one place. There is an enrollment table which lists which users are enrolled as students in a course.

I would like to do something like the following:

class School < ActiveRecord::Base
  has_many :courses
  has_many :students :through enrollments
  has_many :teachers :through courses
end

class Course < ActiveRecord::Base
  has_one :teacher
  belongs_to :school
  has_many :students :through enrollments
end

class User < ActiveRecord::Base
  has_many :courses
  has_many :schools
end

But if I have only a users table and not two separate students and teachers tables, this won't work.

Instead, I would have to do something like

class School < ActiveRecord::Base
  has_many :users [that are teachers]
  has_many :users :through enrollments [that are students]
end

How can I set up my model and associations to make this work?

Thanks.


回答1:


Use inheritance.

Teachers and students are inherited from the users model. You can consult http://api.rubyonrails.org/classes/ActiveRecord/Base.html for further information. Be sure to create a "type" column or equivalent in your User table.

class User < ActiveRecord::Base
end

class Student < User
end

class Teacher < User
end

Rails will treat them individually, but they will still exist in the User table.Let me know if you need further assistance




回答2:


I may have missed something, but it should work if you add the class_name to your relation with "User":

class School < ActiveRecord::Base
  has_many :courses
  has_many :students :through enrollments, :class_name => "User"
  has_many :teachers :through courses, :class_name => "User"
end

class Course < ActiveRecord::Base
  has_one :teacher, :class_name => "User"
  belongs_to :school
  has_many :students :through enrollments, , :class_name => "User"
end

class User < ActiveRecord::Base
  has_many :courses
  has_many :schools
end



回答3:


Add a teachers_id column to courses and use belongs_to instead of has_one. Then add a class_name option.

class School < ActiveRecord::Base
  has_many :courses
  has_many :students :through enrollments
  has_many :teachers :through courses
end

class Course < ActiveRecord::Base
  belongs_to :teacher, :class_name => 'User'
  belongs_to :school
  has_many :students :through enrollments
end

class User < ActiveRecord::Base
  has_many :courses
  has_many :schools, :through enrollments
  has_many :teachers, :through :courses
end


来源:https://stackoverflow.com/questions/8771600/rails-associations-how-can-i-set-up-associations-for-different-varieties-of-us

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!