JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources
Including composed relationships
Nessuno ha ancora preso questa issue.
- Lingua principale
- Ruby
- Stelle
- 2.3k
- Fork
- 546
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
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.
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Leggi lib/jsonapi/resource.rb#L533-L541 e le definizioni di PersonResource e FriendshipResource mostrate nell’issue. Traccia il percorso con cui include=friendships raggiunge records.includes(relationship), quindi determina quale relazione supportata o quale percorso di inclusione può rappresentare entrambe le estremità dell’amicizia. Il lavoro è completato quando la risposta di una persona include tutte le amicizie in un’unica richiesta senza l’errore di associazione mancante.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- rails, ruby
- Ambito
- api, backend
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 25/100