What is the "Rails way" of versioning a table at a set interval?

230 Views Asked by At

I have a model that looks like this:

class WidgetStack < ApplicationRecord
  belongs_to :widget
  belongs_to :person

  validates :quantity, numericality: { greater_than_or_equal_to: 0, only_integer: true }
end

All the table consists of is those two foreign keys and that quantity bigint. quantity can only increase, and potentially changes thousands of times a day.

I want to make a record of the state of the widget_stacks table every hour, on the hour, so that I can report on the changes to each WidgetStack's quantity over the last hour, day, week, month, etc.

My inclination is to make a WidgetStacksHistory model and table, and then use the whenever gem to hourly create a WidgetStackHistory record for every WidgetStack.

I also found the paper_trail gem, which does model versioning, but I don't think it's for me because a WidgetStack's quantity changes so often and I need the record taken every hour on the hour, for every WidgetStack at the same time. There will be a lot of WidgetStacks, and I don't need/want to record every single change. Furthermore, since I need the exact quantity every hour on the hour, if I used paper_trail, I'd have to record every single change to make sure that I had the latest data on the hour.

What's the "Rails way" of taking periodic snapshots of a table like this? Is my WidgetStacksHistory + whenever solution the way to go?

UPDATE with more info:

To clarify regarding the data: When I capture the data, it needs to fully include every WidgetStack, so three bigints (two foreign keys plus quantity), and the created_at timestamp for each WidgetStack. There will potentially be thousands of WidgetStacks. I need to store the data with at least hourly granularity for the last day, daily granularity for the last month, and monthly granularity indefinitely.

0

There are 0 best solutions below