JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Scoping a `has_one` relationship

Open
#627 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
2.3k
Forks
546
PR merge metrics
No merged PRs in 30d

Description

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?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the QuestionResource relationship declaration and the /api/questions/123?include=current_user_answer request described here; reproduce the association-not-found exception and trace how included has_one relationships are resolved. Determine whether context-scoped lookup is supported, and document or implement a canonical way to return the related Answer without an extra request.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
api, backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.