Coder Social home page Coder Social logo

abacus_count's Introduction

Abacus Count

Build Status Code Climate

Synopsis

ActiveRecord::Base#count and other calculations as subqueries. Instead of nice Rails grouping calculation feature, uses subqueries to return the result with any complex query.

There is a use case when ActiveRecord calculations fail on a valid relation. It's when you use alias values you select, and then use aliases in where or having conditions. That happens because ActiveRecord does calculations by throwing away select values. And when really do need to use custom aliases outside select, you definitely do not want this kind of optimization. More on that bug in this pull request.

The issue is fixed, so Rails master and 3.1.1 will be free of it. However, if you are using earlier version of Rails 3å, we can use an ultimate way of preventing any calculation issues, suggested by abacus_count. I mean using subqueries.

Installation

In your Gemfile:

  gem "abacus_count"

and then run bundle.

Usage

You now have abacus scope on any relation. This scope extends a relation with a calculations patch. With it, each calculation will be performed through subquery and so they won't fail in any case.

  users = User.select("users.id, avg(transactions.amount) as avg_amount").joins(:transactions).group("user_id").having("avg_amount >= 15")
  users.count # will fail
  users.abacus.count # will do

  # with Kaminari
  users.page(params[:page]).per(10) # will fail in count
  users.abacus.page(params[:page]).per(10) # will do

Caveats

In abacus calculations never return a hash, always a total result. Sometimes, this is just what you want, however.

Support and contribution

Tested with ruby 1.8.7 and ruby 1.9.3. Tested with ActiveRecord 3.0.9 and 3.1.0.rc5. Great thanks to a wonderful Thoughtbot's Appraisal gem. Tested with sqlite3 and mysql. Currently test suite runs only under sqlite3, gonna implement automated acceptance testing with mysql and maybe postgres.

Contribution

Install to your app and use it! If something bad happens, please, submit an issue or a pull request.

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.