JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Include has_many through: relationship fails

未关闭
#643 8 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
Ruby
星标
2.3k
派生
546
PR 合并指标
30 天内没有已合并 PR

描述

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.

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先,使用 User、Participation 和 Attempt 模型定义重现列出的 GET /users/1 请求,并将 include 响应与直接的 ActiveRecord 查询进行比较。调查经过筛选的 has_many-through 关系的处理方式以及嵌套 include 的顺序;当 completed-attempts 仅返回已完成的 Attempt,且所有列出的请求都避免 500 时,即视为完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
rails, ruby
领域
api, backend
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。