Mongoid Counter Cache is a simple mongoid extension to add basic counter cache functionality to Embedded and Referenced Mongoid Documents.
http://rdoc.info/github/jah2488/mongoid-magic-counter-cache/master/frames
$ [sudo] gem install mongoid_magic_counter_cache
gem '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 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
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"
If you want to maintain counter based on certain condition, then you can specify it using :if
class Post
include Mongoid::Document
field :article
field :comment_count
has_many :comments
end
Then in the referrenced/Embedded document, add condition for counter using :if
class Comment
include Mongoid::Document
include Mongoid::MagicCounterCache
belongs_to :post
field :remark
field :is_published, type: Boolean, default: false
counter_cache :post, :if => Proc.new { |act| (act.is_published) }
end
comment_count will get incremented / decremented only when :if
condition returns true
In conjunction with the conditional counter, if you want to maintain counter after an update to an object, then you can specify it using :if_update
Using same example as above, in the referrenced/Embedded document, add an additional condition for counter using :if_update
class Comment
include Mongoid::Document
include Mongoid::MagicCounterCache
belongs_to :post
field :remark
field :is_published, type: Boolean, default: false
counter_cache :post, :if => Proc.new { |act| (act.is_published) }, :if_update => Proc.new { |act| act.changes['is_published'] }
end
When a comment is saved, comment_count will get incremented / decremented if the is_published field is dirty.
If you'd like to contribute, feel free to fork and merge until your heart is content