Hopefully this is allowed, I know it speaks of a specific problem, but it's also a general problem that others might have to deal with at some point so hoping that this thread could be of some use to others in the future.
In my project I have models who contain many nested relations, all of which themselves contain many nested relations. Including many pivot many-to-many relations. I've been tasked with making it possible to save a snapshot of the top level model, including all of its recursive nested relations. These snapshots, also called baselines or versions, can then be loaded at a later time. They can never be edited however, they are frozen in time.
My first approach was to create a new table for this and save the model and all of its recursive relations as json, with the idea that I could then later hydrate that json back into models and return them via my API. This is proving to be quite tricky as the process of re-hydrating my complex json into models and their relations is cumbersome and feels hacky. The idea with this approach is that I could have my snapshots contained and would not have to pollute my database with tons of "cloned" records.
My second approach would be to create duplicate copies of my models and all their relations whenever a snapshot is created. This feels like it would indeed work, but would result in potentially lots of extra rows, though that in itself shouldn't be much concern.
I was wondering if anyone can think of a better approach to my problem? Or if either of my two above approaches seem sound and valid?
Just thought I would pick some brains! Many thanks for any insight! submitted by /u/spar_x
[link] [comments]