JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Including composed relationships

Open
#744 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Type: Enhancement
Dominant language
Ruby
Stars
2.3k
Forks
546
PR merge metrics
No merged PRs in 30d

Description

I want to find out how to handle symmetric relationships with JSON API (and an Ember.js front-end). I have the following models:

class Person < ActiveRecord::Base
  has_many :friended, through: :friender_friendships, class_name: Person
  has_many :friended_by, through: :friended_friendships, source: :friended, class_name: Person
  has_many :friender_friendships, class_name: Friendship, foreign_key: "friender_id"
  has_many :friended_friendships, class_name: Friendship, foreign_key: "friended_id"

  def friendships
    Friendship.where("friender_id = ? OR friended_id = ?", id, id);
  end

end
class Friendship < ActiveRecord::Base
  belongs_to :friender, class_name: Person
  belongs_to :friended, class_name: Person
end

And the current resources:

class PersonResource < JSONAPI::Resource
  attributes :name
  relationship :friender, to: :many, class_name: "Person"
  relationship :friended_by, to: :many, class_name: "Person"
  relationship :friender_friendships, to: :many, class_name: "Friendship"
  relationship :friended_friendships, to: :many, class_name: "Friendship"
  relationship :friendships, to: :many, class_name: "Friendship"
end
class FriendshipResource < JSONAPI::Resource
  has_one :friender
  has_one :friended
  attributes :strength
end

When looking at a person's profile, I would like to show all friendships of that person, whether they are on the "friender" or the "friended" end of the relationship. When I'm fetching the person resource, I pass include=friendships so that all friendships are also returned and I don't need further requests to fetch friendship data.

However, that fails with the following error:

Association named 'friendships' was not found on Person; perhaps you misspelled it?

The reason for this is that jsonapi_resources eventually calls records.includes(relationship) (see here), and since friendships is not a real relationship, ActiveRecord throws an error.

I'm not really sure how to solve this with jsonapi_resources. It might be possible to turn friendships into a "real" hasMany relationship with AR, but I haven't found a way to do that (although it's possible my AR-fu is just lacking).

If not with AR, is there a way to solve this problem with jsonapi_resources? (without making an extra roundtrip to the server). Thank you.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read lib/jsonapi/resource.rb#L533-L541 and the PersonResource and FriendshipResource definitions shown in the issue. Trace how include=friendships reaches records.includes(relationship), then determine what supported relationship or inclusion path can represent both friendship ends. Done means a person response includes all friendships in one request without the missing-association error.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
api, backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.