JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources
Issue with Updating the foreign key of a polymorphic relationship
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 2.3k
- Forks
- 546
- PR merge metrics
- No merged PRs in 30d
Description
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.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with replace_polymorphic_to_one_link and the Photo, Order, Api::V1::PhotoResource, and Api::V1::OrderResource relationship definitions shown in the issue. Reproduce creating or updating an Order through relationships after uploading a Photo, then verify that the polymorphic link is handled on the correct resource without the OrderResource workaround.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rails, ruby
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100