JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

has_one foreign_key_on: :related fails on save

Open
#670 4 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

When saving a resource with a has_one relationship with foreign_key_on: :related I get an error of undefined method 'foreign_key=' for the model. I'm not sure if the intent is to change the relationship on the related items when save or not. For now I did the following patch to not update any relationships that have foreign_key_on: :related

def replace_to_one_link(relationship_type, relationship_key_value)
  if(self.class._relationships[relationship_type.to_sym]).belongs_to?
    super
  end
end

def replace_polymorphic_to_one_link(relationship_type, relationship_key_value, relationship_key_type)
  if(self.class._relationships[relationship_type.to_sym]).belongs_to?
    super
  end
end

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 by reproducing the save failure for a has_one relationship using foreign_key_on: :related, then trace the relationship save path through replace_to_one_link and replace_polymorphic_to_one_link. Determine the intended behavior for related-item relationships and add or update coverage for the failing case; done means saving no longer calls an unavailable foreign_key= method and the intended relationship behavior is verified.

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
Needs clarification
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.