3

What's the preferred way to do has_one :model, through: join_model in a model resource? Usually JSONAPI::Resource expects model_id column on the table/model who owns the association. That does not exist if a join table/model is used.

jeffdill2
  • 3,968
  • 2
  • 30
  • 47
Yaw Boakye
  • 10,352
  • 1
  • 17
  • 25
  • was my answer able to help you? – jeffdill2 Dec 31 '15 at 02:48
  • I haven't tried it yet. But my question was about `has_one` not `has_many`. – Yaw Boakye Jan 02 '16 at 12:46
  • Oh sorry, I saw 'has' and 'through' and my mind immediately went to `has_many`. So far, I've only needed `has_many` relationships when I've been using `JSONAPI::Resource`. However, since the relationship structure is essentially the same for a `has_one`, I'd imagine the relationship declaration in the resource would just be `has_one` (similar to my answer below), since that's the way it works for `has_many`. – jeffdill2 Jan 05 '16 at 00:17
  • have you had a chance yet to see if using `has_one ` works as you need it to in your resource? – jeffdill2 Jan 11 '16 at 15:01
  • I'm not working on the project now that's why I'm not reacting early to your comments. Forgive me. – Yaw Boakye Jan 12 '16 at 14:12
  • not a problem. Just curious. – jeffdill2 Jan 13 '16 at 18:44

1 Answers1

1

You can actually just mention the has_many relationship, with no need to mention the through association.

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 Teacher resource, all you'd need is has_many :students.

And likewise, in your Student resource, you'd need has_many :teachers.

jeffdill2
  • 3,968
  • 2
  • 30
  • 47