在模型资源中执行 has_one :model, through: join_model
的首选方法是什么?通常 JSONAPI::Resource
期望表/模型上的 model_id
列拥有关联。如果使用连接表/模型,则不存在。
最佳答案
实际上你可以只提到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
。
关于ruby-on-rails - 如何在 JSONAPI::Resource 中执行 `has_one: model, through: join_model`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30002953/