I’m totaling the rows in one of my models using Model.count and am a bit concerned about performance, as eventually, this model will get very large, and, therefore, SELECT COUNT (*) very slow.
Is there a way to use counter_cache without the :belongs_to relationship? Or another performance-friendly way of counting the rows? I thought about making another model, just one where I store calculations like this but not sure that’s the best way.
Even more trivial than making a
Cachemodel is to just useRails.cache.Rails uses a file store by default (tmp/cache).
Then you could just place a Rails.cache.write increment and decrement into your model’s
after_createandafter_destroyhooks, and overrideModel.sizewith a call to Rails.cache.read.You could initialize the cache whenever Rails first initializes by placing a file named something like
initialize_cache.rbin config/initializers containing: