has_one :model, through: join_model
在模型资源中首选的方法是什么?通常JSONAPI::Resource
期望model_id
拥有该关联的表/模型上的列。如果使用连接表/模型,则不存在。
问问题
453 次
1 回答
1
您实际上可以只提及has_many
关系,而无需提及through
关联。
所以如果你有这个模型结构:
class Teacher < ActiveRecord::Base
has_many :classrooms
has_many :students, through: :classrooms
end
class Student < ActiveRecord::Base
has_many :classrooms
has_many :teachers, through: :classrooms
end
class Classroom < ActiveRecord::Base
belongs_to :teacher
belongs_to :student
end
在您的Teacher
资源中,您只需要has_many :students
.
同样,在您的Student
资源中,您需要has_many :teachers
.
于 2015-12-22T16:55:15.590 回答