You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
The text was updated successfully, but these errors were encountered:
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?
The text was updated successfully, but these errors were encountered: