JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Issue with Updating the foreign key of a polymorphic relationship

Offen
#626 2 Kommentare 4 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Type: Bug
Vorherrschende Sprache
Ruby
Sterne
2.3k
Forks
546
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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.

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit replace_polymorphic_to_one_link und den im Issue gezeigten Beziehungsdefinitionen für Photo, Order, Api::V1::PhotoResource und Api::V1::OrderResource. Reproduziere das Erstellen oder Aktualisieren einer Order über Beziehungen, nachdem ein Photo hochgeladen wurde, und überprüfe anschließend, dass der polymorphe Link auf der korrekten Ressource verarbeitet wird, ohne den OrderResource-Workaround.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
rails, ruby
Bereich
api, backend
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.