Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
jah2488 committed Nov 19, 2012
1 parent 7ae08e5 commit 0256937
Showing 1 changed file with 32 additions and 27 deletions.
59 changes: 32 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,54 @@ Mongoid Counter Cache is a simple mongoid extension to add basic counter cache f
## INSTALLATION

### RubyGems

$ [sudo] gem install mongoid_magic_counter_cache

````sh
$ [sudo] gem install mongoid_magic_counter_cache
````
### GemFile

gem 'mongoid_magic_counter_cache'

````rb
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
````rb
class Library
include Mongoid::Document

field :name
field :city
field :book_count
has_many :books

end
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
````rb
class Book
include Mongoid::Document
include Mongoid::MagicCounterCache

field :first
field :last

=> @library.book_count
=> 990
belongs_to :library
counter_cache :library
end
````

````rb
$ @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"
````rb
counter_cache :library, :field => "total_amount_of_books"
````

## TODO

Expand Down

0 comments on commit 0256937

Please sign in to comment.