DmitryTsepelev / store_model

Work with JSON-backed attributes as ActiveRecord-ish models
MIT License
1.04k stars 85 forks source link

accepts_nested_attributes_for allow partial updates #157

Closed acetinick closed 1 week ago

acetinick commented 10 months ago

When using accepts_nested_attributes_for on models the JSON objects gets overidden completely with the next update that happens on the model.

eg. if you have two attributes in the store, and update each atrtibute using two separate forms, it will override the whole store with only the new attribute.

To fix this I have a override in my model, but feel it should be a default for the library.

image

DmitryTsepelev commented 10 months ago

Hey hey, good call! Wanna try to make a PR to make it work on the gem level? 🙂

alexeevit commented 7 months ago

Good call! At the same time, what do we do if we indeed want to override the whole hash with the new hash, e.g. delete some of the attributes? I was thinking about an attribute _partial that'd be passed with the hash, so it could indicate only partial update, but I don't like interfering with the original data.

briandunn commented 1 month ago

Rails achieves this by

1) presence or absence of an id , and 2) an update_only flag.

https://api.rubyonrails.org/v7.1.3.4/classes/ActiveRecord/NestedAttributes/ClassMethods.html

An implementation like that would keep with active record api expectations.

DmitryTsepelev commented 1 week ago

Likely gonna be addressed by #180