JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Scoping a `has_one` relationship

Aperta
#627 6 commenti 0 reazioni 0 assegnatari Vedi su GitHub

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

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?

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con la dichiarazione della relazione QuestionResource e la richiesta /api/questions/123?include=current_user_answer descritta qui; riproduci la association-not-found exception e traccia il modo in cui vengono risolte le relazioni has_one incluse. Determina se è supportata la ricerca con ambito contestuale e documenta o implementa un modo canonico per restituire l’Answer correlato senza una richiesta aggiuntiva.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.