3

has_one :model, through: join_model在模型资源中首选的方法是什么?通常JSONAPI::Resource期望model_id拥有该关联的表/模型上的列。如果使用连接表/模型,则不存在。

4

1 回答 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 回答