JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Support foreign_type in has_one association

Open
#875 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
2.3k
Forks
546
PR merge metrics
No merged PRs in 30d

Description

I am updating an app to use jsonapi:resources and have had to change a model association name without changing the underlying table. This model had a polymorphic field :resource which is now called :item, however the underlying table still has fields resource_id and resource_type.

In my model I can do this:

belongs_to :item, polymorphic: true, foreign_key: "resource_id", foreign_type: "resource_type"

However when I do this in my resource file:

has_one :item, polymorphic: true, foreign_key: 'resource_id', foreign_type: 'resource_type'

I receive an error "undefined method item_type=". From the docs it looks like foreign_key is supported but foreign_type is not. Could this be easily fixed?

Thanks for a great gem, it's been a pleasure to work with!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the has_one association handling and the existing foreign_key support, then reproduce the model and resource configurations shown in the issue. Done means foreign_type is accepted without calling the undefined item_type= method, with coverage for polymorphic associations whose model name differs from the underlying type column.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
api, backend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.