How to do `has_one: model, through: join_model` in JSONAPI :: Resource

What's the preferred way to do has_one :model, through: join_model

in a model resource? Usually JSONAPI::Resource

expects the column model_id

in the table / model that the association belongs to. This does not exist if table / join model is used.

+3


source to share


1 answer


In fact, you can just mention the relationship has_many

without having to mention the association through

.

So if you had this model structure:

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

      



In your resource, Teacher

all you need is this has_many :students

.

And also in your resource Student

you will need has_many :teachers

.

+1


source







All Articles