Mongoid Counter Cache is a simple mongoid extension to add basic counter cache functionality to Embedded and Referenced Mongoid Documents.
$ [sudo] gem install mongoid_magic_counter_cache
First add a field to the document where you will be accessing the counter cache from.
class Library include Mongoid::Document field :name field :city field :book_count has_many :books end
Then in the referrenced/Embedded document. Include
class Book include Mongoid::Document include Mongoid::MagicCounterCache field :first field :last belongs_to :library counter_cache :library end => @library.book_count => 990
If you do not wish to use the
model_count naming convention, you can override the defaults by specifying the
counter_cache :library, :field => "total_amount_of_books"
- Add additional options parameters
- Simplify syntax (I.E. including MagicCounterCache will add counts for all
belongs_toassociations on a document
If you'd like to contribute, feel free to fork and merge until your heart is content