JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Related resources not backed by an association

未关闭
#545 4 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

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

描述

Hello, I'm curious to see if anyone is interested in allowing a resource to have a relationship that isn't necessarily an association to the underlying model. For example:

A Video has_many :tags. I've created an instance method (Video#related_videos) that pulls related videos based on a video's set of tags.

video.related_videos
=> [related_video_1, related_video_2]

What I'm interested in is having the corresponding VideoResource aware that this isn't an association it should be looking for, but rather public method. This would allow the following requests:

videos/:id?include=related_videos
videos/:id/relationships/related_videos

Currently, an InvalidInclude exception will be raised since "related_videos is not a valid relationship of videos." What if we were to pass a flag to relationship / has_many / has_one? Maybe there's a better way, but I was thinking something like:

VideoResource < JSONAPI::Resource
  has_many :related_videos, instance_method: true
end

Would something like this be worth exploring? I'd be happy to work on a PR to support something like this if there's any interest.

贡献指南

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

从这里开始

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

调研方向

首先查看 issue 中描述的资源关系声明(relationshiphas_manyhas_one)以及 InvalidInclude 路径。确定如何通过两个示例请求表示并公开类似 Video#related_videos 的公共方法;完成标准要求就 API 和验证行为达成一致。

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

评估

技术栈
rails, ruby
领域
api, backend
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

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