Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a record to a has_many relationship should be able to POST #241

Open
JohnSmall opened this issue Feb 27, 2017 · 1 comment
Open
Assignees

Comments

@JohnSmall
Copy link

The JSON-API spec regarding relationships allows both PATCH and POST for updating a relationship.

If a PATCH is used then the entire contents of the relationship are replaced. If a POST is use then only the new members are added.

Consider a situation where one object has_many possibly hundreds of linked objects. If I want to add an object to that relationship then I have to load all the existing objects, add my new one, and then save the top level object. If I just add the one new object and save, then all the existing objects get removed from the relationship.

For cases where the relationship is has_many <-> has_one, then I can go to the has_one side and add the owner object and save (though this has problems see #226 ) but if I have a many to many relationship, with each side having potentially hundreds to link to, this is going to be a problem.

I can see in the code for def save, it checks if the object is new or already loaded. If it's new it POSTs, and if already loaded it PATCHes. Which is correct, except for this one case.

The current behaviour is to PATCH on the has_many relationship, because it's an update to an existing object, but that wipes out any existing relations.

The only way I can see around this is that an extra method could be added to a has_many relationship #add_this(new_member), which then does a POST to the has_many relationship to add the new member, leaving the owner object alone.

Any thoughts from anyone?

@gaorlov gaorlov self-assigned this Mar 20, 2019
@gaorlov
Copy link
Collaborator

gaorlov commented Mar 20, 2019

Oh. That's not great. I'll take a look. Thanks for bringing this up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants