Giter Site home page Giter Site logo

association_count's Introduction

AssociationCount Build Status

Get ActiveRecord association count with ease and without worrying about N+1 queries:

Author.all.include_post_count.map(&:post_count)

A small gem for ActiveRecord that allows association counts to be included in your base query.

Installation

Add this line to your application's Gemfile:

gem 'association_count'

And then execute:

$ bundle

Or install it yourself as:

$ gem install association_count

Usage

Include in specific model

Simply add

class Post < ApplicationRecord
  extend AssociationCount

  # [...]
end

Include in all models

Rails 5, add it to ApplicationRecord

class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true
  # [...]
  extend AssociationCount
end

Rails 4, add it to ActiveRecord::Base

ActiveRecord::Base.extend AssociationCount

Full example

class Foo < ActiveRecord::Base
  has_many  :bars
  can_count :bars
end

class Bar < ActiveRecord::Base
  belongs_to  :foo
end

# Each Foo instance will come with a "preloaded" count method: bar_count
Foo.all.include_bar_count.map(&:bar_count) # only one SQL query executed

# you can also achieve the same with
foos = Foo.all.association_count(Bar)

This works for any has_many relationship even if it uses non standard foreign keys or is a has_many :x, through: y.

ℹī¸ By default we will use left outer join and not distinct.

You can configure this on per model basis

class Foo < ActiveRecord::Base
  has_many  :bars
  can_count :bars, distinct: true, join_type: :joins # can also be left_outer_joins
end

or on a case by case basis

Foo.all.include_bar_count(distinct: false, join_type: :left_outer_joins)

Configuration

AssociationCount.configure do |config|
  config.distinct = false
  config.join_type = :joins # or left_outer_joins
end

Development

After checking out the repo, run bin/setup to install dependencies. Then, run bin/console for an interactive prompt that will allow you to experiment.

To 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.

Contributing

  1. Fork it ( https://github.com/buren/association_count/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

association_count's People

Contributors

albin-willman avatar buren avatar lostapathy avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

lostapathy

association_count's Issues

Other aggregations?

Would you have any interest in a PR that adds support for other aggregations, or would you rather leave the scope of this gem as-is?

It would be really awesome to be able to have (roughly):

class Invoice < ActiveRecord::Base
  has_many :charges
  can_aggregate :charges, :total_charge_amount, "sum(quantity * amount)"
end

class Charge < ActiveRecord::Base
  belongs_to :invoice
end

Invoice.include_total_charge_amount

Or perhaps have various helpers such as this to have a nicer, if more limited, DSL.

can_sum :charges, :total_charge_amount, "quantity * amount"

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤ī¸ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.