学校、コース、学生、教師で構成される Web アプリを作成しています。
学校には多くのコースがあり、コースには 1 人の教師と多くの生徒がいます。
私が直面している問題は、1 人のユーザーが 1 つのコースの教師であるが、別のコースの学生 (または別の学校のコースの学生または教師) である可能性があることです。すべてのユーザーを 1 か所で追跡したいので、教師用のモデルと学生用の別のモデルを作成したくありません。コースに学生として登録されているユーザーを一覧表示する登録テーブルがあります。
次のようなことをしたいと思います。
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
しかし、users テーブルだけがあり、2 つの個別の学生と教師のテーブルがない場合、これは機能しません。
代わりに、次のようなことをしなければなりません
class School < ActiveRecord::Base
has_many :users [that are teachers]
has_many :users :through enrollments [that are students]
end
これを機能させるためにモデルと関連付けを設定するにはどうすればよいですか?
ありがとう。