counter_culture

Turbo-charged counter caches for your Rails app.

What is counter_culture?

Turbo-charged counter caches for your Rails app.

Overview of counter_culture

What it is

counter_culture is a Ruby gem that provides turbo-charged counter caches for Rails applications. It improves upon Rails' standard counter caches by updating the cache when values change (not just on create and destroy), supporting multi-level relations, dynamic column names, running counts, and running totals. It is tested against Ruby 3.0 through 4.0 and Rails 6.0 through 8.1. Unlike Rails' built-in counter caches, counter_culture does not change the behavior of the .size method on ActiveRecord associations; to read the cached value without a query, the attribute name containing the counter cache must be used directly (e.g., product.categories_count instead of product.categories.size).

Key features

  • Updates counter cache when values change, not just when creating and destroying records.
  • Supports counter caches through multiple levels of relations (e.g., through sub_category to category).
  • Supports dynamic column names, allowing the counter cache to be split for different types of objects.
  • Can keep a running count or a running total (using the :delta_column option).
  • Supports conditional counter caches (e.g., only count records where a condition is true).
  • Allows temporarily skipping counter cache updates via Review.skip_counter_culture_updates.
  • Supports delta magnitude (static or dynamic multipliers) for the counter change.

Use cases

counter_culture is used in Rails applications where accurate, up-to-date counter caches are needed across complex associations. Common use cases include:

  • Keeping a count of products in a category, updated when a product's category changes.
  • Maintaining a count of members in a group through a join table (many-to-many).
  • Tracking a running total (e.g., sum of product weights) in a parent model.
  • Splitting counter caches by a dynamic attribute (e.g., counting only 'awesome' products separately).
  • Updating counters across multiple levels of a hierarchy (e.g., sub_category and category).

Getting started

Add counter_culture to your Gemfile:rubygem 'counter_culture', '~> 3.2'Then run bundle install.Create the necessary columns using the generator:rails generate counter_culture Category products_countThis generates a migration with code like:rubyadd_column :categories, :products_count, :integer, null: false, default: 0Note: The column must be NOT NULL and have a default of zero.

License and pricing

counter_culture is released under the MIT license. It is open source and free to use.

Based on the project's public README and repository metadata.

Add this badge to your README

Featured on YunoDB Badge styles & help

Maintain this project? Show that it is featured on YunoDB.

Markdown[![Featured on YunoDB](https://yunodb.com/badge/magnusvkcounter-culture.svg)](https://yunodb.com/tools/magnusvkcounter-culture?utm_source=github&utm_medium=badge)
HTML<a href="https://yunodb.com/tools/magnusvkcounter-culture?utm_source=github&utm_medium=badge"><img src="https://yunodb.com/badge/magnusvkcounter-culture.svg" alt="Featured on YunoDB" height="20"></a>