Rails storing aggregated information Rails storing aggregated information database database

Rails storing aggregated information


I've been working on a similar problem and I've also worked on applications that did this the wrong way.

Here is my best-practices recommendation:

  • Store the raw data in the model, let's call is Feed
  • Set up a one-on-one association to another model that holds the computed values, e.g. FeedStats. This may also be a one-to-many association or many-to-one, depending on the exact case; you might roll up some individual Feed records into some sort of aggregate, etc.
  • Keep all original raw data around. This will be extremely useful if you later want to switch the computation to some other algorithm and you may need to recompute old data, or if you discover bugs in the computation, etc.
  • Set up the computation on a background task, using tools like Resque (with or without Scheduler), DelayedJob or similar.

If you can be a bit more specific and give some examples of your exact problem, I can perhaps give some more specific tips. Good luck.