Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
A Simple Counter Cache Gem for the Mongoid ORM
Ruby
Branch: master

This branch is 33 commits behind jah2488:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
spec
.gitignore
.rspec
.travis.yml
CHANGELOG.md
Gemfile
LICENSE
README.md
Rakefile
mongoid_magic_counter_cache.gemspec

README.md

Mongoid Magic Counter Cache Build Status

DESCRIPTION

Mongoid Counter Cache is a simple mongoid extension to add basic counter cache functionality to Embedded and Referenced Mongoid Documents.

RDOC

http://rdoc.info/github/jah2488/mongoid-magic-counter-cache/master/frames

INSTALLATION

RubyGems

$ [sudo] gem install mongoid_magic_counter_cache

GemFile

gem 'mongoid_magic_counter_cache'

USAGE

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 Mongoid::MagicCounterCache

class Book
  include Mongoid::Document
  include Mongoid::MagicCounterCache

  field :first
  field :last

  belongs_to    :library
  counter_cache :library
end


=> @library.book_count
=> 990

Alternative Syntax

If you do not wish to use the model_count naming convention, you can override the defaults by specifying the :field parameter.

counter_cache :library, :field => "total_amount_of_books"

TODO

  1. Add additional options parameters
  2. Simplify syntax (I.E. including MagicCounterCache will add counts for all belongs_to associations on a document

CONTRIBUTE

If you'd like to contribute, feel free to fork and merge until your heart is content

Something went wrong with that request. Please try again.