JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Include has_many through: relationship fails

Open
#643 8 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

With the following set of models:

class User < ActiveRecord::Base
    ...
    has_many :participations
    has_many :attempts, through: :participations
    has_many :completed_attempts, -> { completed }, through: :participations, source: :attempts
    ...
end
class Participation < ActiveRecord::Base
    ...
    belongs_to :user
    has_many :attempts
    ...
end
class Attempt < ActiveRecord::Base
    ...
    belongs_to :participation
    ...
end

the request GET /users/1?include=completed-attempts fails. completed-attempts.txt.

Likewise, GET /users/1?include=completed-attempts,attempts fails. completed-attempts_attempts.txt

However, as expected, GET /users/1?include=attempts works. attempts.txt

Changing the order, GET /users/1?include=attempts,completed-attempts, doesn't fail with a 500, however it returns the full list of Attempts for both the attempts and completed-attempts relationships. attempts_completed-attempts.txt

Querying the relations directly from the AR object works correctly for both attempts and completed_attempts.

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 by reproducing the listed GET /users/1 requests with the User, Participation, and Attempt model definitions, comparing the include responses with direct ActiveRecord queries. Investigate the handling of filtered has_many-through relationships and nested include order; done means completed-attempts returns only completed attempts and all listed requests avoid a 500.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.