JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources
Scoping a `has_one` relationship
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
After searching for a while, the answer here isn't obvious to me, so I thought I'd bring it up as an issue for reference.
I have 3 models:
class User < ActiveRecord::Base
has_many :answers
end
class Question < ActiveRecord::Base
has_many :answers
end
class Answer < ActiveRecord::Base
belongs_to :user
belongs_to :question
end
What I'd like to do is include the current_user_answer in a questions#show request, like so: /api/questions/123?include=current_user_answer. It seems like I should be able to write my QuestionResource as follows:
class QuestionResource < JSONAPI::Resource
has_many :answers
has_one :current_user_answer, class_name: 'Answer'
# doesn't get called
def self.record_for_current_user_answer(options={})
current_user = options[:context][:current_user]
AnswerResource.records(options).find_by(
question: @model,
user: current_user
)
end
# doesn't get called either
def current_user_answer(options={})
current_user = options[:context][:current_user]
AnswerResource.records(options).find_by(
question: @model,
user: current_user
)
end
end
Neither of these methods get called; instead, I get an exception "Association named 'current_user_answer' was not found on Question; perhaps you misspelled it?" Well, by definition here I'm trying to avoid writing a query method directly on the model, and anyways such a query method or association would need to get the context passed in from the enclosing Resource.
In short, there seems to be no documented way to scope an included associated to something in the context. Or am I missing something? What's the canonical way to get this related resource without an extra request?
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
Comienza con la declaración de la relación de QuestionResource y la solicitud /api/questions/123?include=current_user_answer descrita aquí; reproduce la association-not-found exception y sigue cómo se resuelven las relaciones has_one incluidas. Determina si se admite la búsqueda con ámbito de contexto y documenta o implementa una forma canónica de devolver el Answer relacionado sin una solicitud adicional.
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