JSONAPI-Resources / JSONAPI-Resources/jsonapi-resources

Remove to many issue using mongoid

Open
#724 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I ran into an issue when trying to remove a has_many relationship using mongoid backed models.

The issues comes down to this code:

def _remove_to_many_link(relationship_type, key)
  relation_name = self.class._relationships[relationship_type].relation_name(context: @context)

  @model.public_send(relation_name).delete(key)

  :completed
end

Mongoid defines the delete method on a relationship as follow https://github.com/mongodb/mongoid/blob/v5.1.2/lib/mongoid/relations/referenced/many.rb#L103

I had to override the method definition in my resource like this:

def _remove_to_many_link(relationship_type, key)
  relation_name = self.class._relationships[relationship_type].relation_name(context: @context)
  relationship = @model.public_send(relation_name)
  document = relationship.find(id: key)

  relationship.delete(document)

  :completed
end

Not a huge deal but it would interesting to find a way to abstract how we delete relationships.

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 at _remove_to_many_link and compare its relationship.delete(key) call with Mongoid's referenced-many delete contract. Identify the abstraction boundary for relationship deletion; done means has_many removal works with Mongoid without a resource override while existing relationship behavior remains intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.