Is there any gem for this? For now I coded my own update_safely method which generates the proper SQL.
--- Mario On Sunday, March 4, 2018 at 4:38:11 AM UTC+1, Jonathan Rochkind wrote: > > It is now easy to store a hash serialized as jsonb in posgres. It is even > pretty easy with store_accessor to make individual keys in the hash look > like separate attributes. > > But when AR decides the jsonb column needs to be updated, it will send the > entire hash as an update value. If you only changed a key or two, it will > still send the entire hash -- possibly overwriting changes to other keys > made by other processes/clients. > > Recent versions of postgres supports an atomic update on just certain keys > of a jsonb hash, using `jsonb_set` in SQL. > > I'd like to explore trying to get AR to do this, at first with a plugin. > With dirty tracking, it's fairly straightforward for AR to figure out what > top-level keys have been changed and what haven't. > > But I can't quite figure out where to intervene in AR to change the SQL > generated in an 'update' operation. So I can generate different SQL that > will do an atomic update, where possible. > > If anyone could give me any advice on where might work to hook into AR to > change generated SQL for 'update', I would appreciate it! And I hope to > look into making a plugin to let AR do specified-key-only updates to > postgres jsonb columns. Thank you for any advice! > -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscr...@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at https://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/d/optout.