title: Implementing custom versioning of a model minimising performance impact
body: <p>I've got a Rails application set up and running just right in development mode. Its data model is quite simple, but has 3 of the models representing 90% of the whole data, which are:</p>
  
  <ul>
  <li>Model A: 3k records</li>
  <li>Model C: 1k records</li>
  <li>Model B: 45k records(which is a rich n to n association between the previous two)</li>
  </ul>
  
  <p>I use MySQL, but probably going to switch to PostgreSQL in production.</p>
  
  <p>All this data comes from external webservices in the form of JSON serialised Model A, with B and C nested inside. I've got a method that creates/updates them in my local database. The thing is, the webservice provides a VERSION parameter of this Model A, meaning every time that changes are made, it increases the version of the data it sends.</p>
  
  <p><img src="https://i.stack.imgur.com/KYKwY.png" alt="enter image description here"></p>
  
  <p>I don't support versioning from my side right now, and I'd like to be able to store as many versions of the data as the webservice gives me (checking every certain time). I'd like to be able to generate a version myself (from my backend) with changes I think need to be made.</p>
  
  <p>I thought of adding a new boolean property in Model A (active), and duplicating Model A every time I get a new version. Model C is never versioned, so this operation will increase the number of records of Model A, and consequently Model B, as it'd need to duplicate all relationships to Model C.</p>
  
  <p><img src="https://i.stack.imgur.com/FbCAP.png" alt="enter image description here"></p>
  
  <p>This is fine so far but, considering I perform lots of statistical operations on Model C with varying criteria (regarding count of relations with model A, which is to count Model B), in this scenario I'd need to filter Model B by Model A's active property, which I can do with a JOIN statement, but I'm not sure if it will affect a lot to performance.</p>
  
  <p>Another option is to popullate Model B with another active property whenever the active Model A is changed, but this sound like a really bad idea DB-wise, as it means duplicating data...</p>
  
  <p>If I went for the first option, would it be a good idea putting a default scope in models A and B in Rails, so that I can "forget about" the data versioning? The default scope would filter by active, either in each model, or with a JOIN statement in Model B.</p>
  
  <p>I might not be the best putting all this into words, but I'd really appreciate any help you're able to give me. I'll be checking the post so that I can clarify any doubts that may arise from my potentially poor explanation!</p>
  
  <p>Thank you so much!</p>