{"id":16679872,"url":"https://github.com/buren/association_count","last_synced_at":"2025-04-09T22:33:36.921Z","repository":{"id":30857092,"uuid":"34414629","full_name":"buren/association_count","owner":"buren","description":"Small gem to include association counts where they are needed.","archived":false,"fork":false,"pushed_at":"2021-11-29T10:05:38.000Z","size":45,"stargazers_count":3,"open_issues_count":1,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-14T19:45:54.634Z","etag":null,"topics":["activerecord","rubygem"],"latest_commit_sha":null,"homepage":null,"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/buren.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2015-04-22T20:39:04.000Z","updated_at":"2022-01-15T09:34:07.000Z","dependencies_parsed_at":"2022-09-03T09:02:32.612Z","dependency_job_id":null,"html_url":"https://github.com/buren/association_count","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buren%2Fassociation_count","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buren%2Fassociation_count/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buren%2Fassociation_count/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buren%2Fassociation_count/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/buren","download_url":"https://codeload.github.com/buren/association_count/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248123995,"owners_count":21051573,"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":["activerecord","rubygem"],"created_at":"2024-10-12T13:37:57.664Z","updated_at":"2025-04-09T22:33:36.896Z","avatar_url":"https://github.com/buren.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AssociationCount [![Build Status](https://travis-ci.org/buren/association_count.svg?branch=master)](https://travis-ci.org/buren/association_count)\n\nGet ActiveRecord association count with ease and without worrying about N+1 queries:\n```ruby\nAuthor.all.include_post_count.map(\u0026:post_count)\n```\n\nA small gem for ActiveRecord that allows association counts to be included in your base query.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'association_count'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install association_count\n\n## Usage\n\n__Include in specific model__\n\nSimply add\n\n```ruby\nclass Post \u003c ApplicationRecord\n  extend AssociationCount\n\n  # [...]\nend\n```\n\n__Include in all models__\n\nRails 5, add it to `ApplicationRecord`\n\n```ruby\nclass ApplicationRecord \u003c ActiveRecord::Base\n  self.abstract_class = true\n  # [...]\n  extend AssociationCount\nend\n```\n\nRails 4, add it to `ActiveRecord::Base`\n\n```ruby\nActiveRecord::Base.extend AssociationCount\n```\n\nFull example\n```ruby\nclass Foo \u003c ActiveRecord::Base\n  has_many  :bars\n  can_count :bars\nend\n\nclass Bar \u003c ActiveRecord::Base\n  belongs_to  :foo\nend\n\n# Each Foo instance will come with a \"preloaded\" count method: bar_count\nFoo.all.include_bar_count.map(\u0026:bar_count) # only one SQL query executed\n\n# you can also achieve the same with\nfoos = Foo.all.association_count(Bar)\n```\n\nThis works for any `has_many` relationship even if it uses non standard foreign keys or is a `has_many :x, through: y`.\n\n:information_source: By default we will use left outer join and __not__ distinct.\n\nYou can configure this on per model basis\n```ruby\nclass Foo \u003c ActiveRecord::Base\n  has_many  :bars\n  can_count :bars, distinct: true, join_type: :joins # can also be left_outer_joins\nend\n```\n\nor on a case by case basis\n\n```ruby\nFoo.all.include_bar_count(distinct: false, join_type: :left_outer_joins)\n```\n\n## Configuration\n\n```ruby\nAssociationCount.configure do |config|\n  config.distinct = false\n  config.join_type = :joins # or left_outer_joins\nend\n```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\n1. Fork it ( https://github.com/buren/association_count/fork )\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fburen%2Fassociation_count","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fburen%2Fassociation_count","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fburen%2Fassociation_count/lists"}