Skip to content

Issue with Updating the foreign key of a polymorphic relationship #626

Open
@acid

Description

@acid

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions