JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources
Including composed relationships
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Ruby
- Estrellas
- 2.3k
- Forks
- 546
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Descripción
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.
Guía de contribución
No hay ninguna guía de contribución indexada para este repositorio
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Lee lib/jsonapi/resource.rb#L533-L541 y las definiciones de PersonResource y FriendshipResource mostradas en el issue. Traza cómo include=friendships llega a records.includes(relationship) y, después, determina qué relación admitida o ruta de inclusión puede representar ambos extremos de la amistad. Se considera terminado cuando la respuesta de una persona incluye todas las amistades en una sola solicitud sin el error de asociación faltante.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- rails, ruby
- Área
- api, backend
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Necesita aclaración
- Aptitud para principiantes
- 25/100