JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Including composed relationships

オープン
#744 コメント 5 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Type: Enhancement
主要言語
Ruby
スター
2.3k
フォーク
546
PR マージ指標
30日以内にマージされた PR はありません

説明

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.

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

lib/jsonapi/resource.rb#L533-L541 と、issue に示されている PersonResource および FriendshipResource の定義を読みます。include=friendships が records.includes(relationship) に到達する経路を追跡し、そのうえで、両方の friendship の端点を表現できるサポート対象の relationship または inclusion path を特定します。完了条件は、person のレスポンスが、missing-association error なしに1回のリクエストですべての friendship を含むことです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
rails, ruby
領域
api, backend
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。