JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Issue with Updating the foreign key of a polymorphic relationship

オープン
#626 コメント 2 件 リアクション 4 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Type: Bug
主要言語
Ruby
スター
2.3k
フォーク
546
PR マージ指標
30日以内にマージされた PR はありません

説明

Hello,

we encountered this issue with an setup like:

class Photo < ActiveRecord::Base
  belongs_to :imageable, polymorphic: true
  ...
end

class Order < ActiveRecord::Base
  ...
  has_one :photo, as: :imageable
  ...
end

class Api::V1::PhotoResource < JSONAPI::Resource
  ...
  has_one :imageable, polymorphic: true
  ...
end

class Api::V1::OrderResource < JSONAPI::Resource
  ...
  has_one :photo, polymorphic: true
  ...
end

The idea is, that you can upload a photo separately to the photo endpoint and then link it to the order while creating/updating it via relationships.

Some debugging revealed that it's recognised as polymorphic but replace_polymorphic_to_one_link is always called on the OrderResource, even with has_one :photo, polymorphic: true, foreign_key_on: :related in the OrderResource.

I would like to fix this behaviour myself, when somebody can point me in the right direction. Until then a workaround for this is:

def replace_polymorphic_to_one_link(relationship_type, relationship_key_value, relationship_key_type)
  @model.photo = Photo.find(relationship_key_value)
end

in the OrderResource.

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

replace_polymorphic_to_one_link と、issue に示されている Photo、Order、Api::V1::PhotoResource、Api::V1::OrderResource のリレーション定義から始めます。Photo をアップロードした後、リレーションを介して Order を作成または更新する処理を再現し、OrderResource の回避策なしでポリモーフィックリンクが正しいリソース上で処理されることを確認します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
rails, ruby
領域
api, backend
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。