{"id":24548168,"url":"https://github.com/flanker/mongoid-categorized_counter_cache","last_synced_at":"2025-06-20T21:35:00.889Z","repository":{"id":48605286,"uuid":"146693701","full_name":"flanker/mongoid-categorized_counter_cache","owner":"flanker","description":"Categorized counter cache for Ruby Mongoid models","archived":false,"fork":false,"pushed_at":"2022-07-24T02:22:04.000Z","size":34,"stargazers_count":1,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-16T15:11:57.549Z","etag":null,"topics":["counter-cache","mongoid","mongoid-plugin","ruby","rubygem"],"latest_commit_sha":null,"homepage":"https://rubygems.org/gems/mongoid-categorized_counter_cache","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/flanker.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-30T04:01:25.000Z","updated_at":"2022-07-24T02:22:03.000Z","dependencies_parsed_at":"2022-09-10T09:01:52.501Z","dependency_job_id":null,"html_url":"https://github.com/flanker/mongoid-categorized_counter_cache","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/flanker/mongoid-categorized_counter_cache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flanker%2Fmongoid-categorized_counter_cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flanker%2Fmongoid-categorized_counter_cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flanker%2Fmongoid-categorized_counter_cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flanker%2Fmongoid-categorized_counter_cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/flanker","download_url":"https://codeload.github.com/flanker/mongoid-categorized_counter_cache/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flanker%2Fmongoid-categorized_counter_cache/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261020660,"owners_count":23098200,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["counter-cache","mongoid","mongoid-plugin","ruby","rubygem"],"created_at":"2025-01-22T23:16:15.290Z","updated_at":"2025-06-20T21:34:55.872Z","avatar_url":"https://github.com/flanker.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mongoid-categorized_counter_cache\n\n[![Build Status](https://www.travis-ci.com/flanker/mongoid-categorized_counter_cache.svg?branch=master)](https://www.travis-ci.com/flanker/mongoid-categorized_counter_cache)\n\nEnhancement to `counter cache` for `mongoid`\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'mongoid-categorized_counter_cache'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install mongoid-categorized_counter_cache\n\n## Usage\n\nWhen you have a one-to-many relation using mongoid, you can define a `counter cache` attribute for the relation.\n\n```ruby\nclass Author\n  include Mongoid::Document\n\n  has_many :books\nend\n\nclass Book\n  include Mongoid::Document\n  \n  belongs_to :author, counter_cache: true\nend\n```\n\nThen:\n\n```ruby\nauthor = Author.create\nauthor.books.create\n\nauthor.books_count\n# =\u003e 1\n# returns a cached count of author.books\n```\n\n`mongoid-categorized_counter_cache` helps to you cache the count of children documents by given category (any attribute of child document)\n\n```ruby\nclass Author\n  include Mongoid::Document\n\n  has_many :books\nend\n\nclass Book\n  include Mongoid::Document\n  include Mongoid::CategorizedCounterCache\n  \n  field :genre\n  \n  belongs_to :author, counter_cache: true\n\n  categorized_counter_cache :author do |book|\n    book.genre\n  end\nend\n```\n\nThen:\n\n```ruby\nauthor = Author.create\nauthor.books.create genre: 'fiction'\nauthor.books.create genre: 'drama'\n\nauthor.books_count\n# =\u003e 2\n# returns a cached count of all author.books\n\nauthor.books_fiction_count\n# =\u003e 1\n# returns a cached count of all author.books which has a fiction genre\n\nauthor.books_drama_count\n# =\u003e 1\n# returns a cached count of all author.books which has a drama genre\n```\n\n[See spec test for more info](spec/mongoid/categorized_counter_cache_spec.rb)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflanker%2Fmongoid-categorized_counter_cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflanker%2Fmongoid-categorized_counter_cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflanker%2Fmongoid-categorized_counter_cache/lists"}