0.0
No commit activity in last 3 years
No release in over 3 years
Simple implementation of counter_cache functionality for model scopes
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 1.0.0
>= 0

Runtime

 Project Readme

scope_counter is a simple implementation of counter_cache functionality for models scopes with ActiveRecord

Install

gem 'scope_counter'

and run bundle install from your shell.

Usage

Create columns in db according to scopes in your model ("#{scope_name}_#{table_name}_count"):

t.integer :usefull_utilities_count, :default => 0
t.integer :useless_utilities_count, :default => 0
t.integer :utilities_count, :default => 0

List in array scopes, which you want to create counter cache for, as :conter_cache option (if you want to add a default behavior just add :all in array):

class Advice  < ActiveRecord::Base
  has_many :utilities
end

class Utility  < ActiveRecord::Base
  belongs_to :advice, :counter_cache => [:all,:usefull, :useless]
  scope :usefull, where(:usefull => true)
  scope :useless, where(:usefull => false)
end

advice = Advice.create

advice.usefull_utilities_count # => 0

advice.utilities.usefull.create

advice.usefull_utilities_count # => 1

advice.utilities_count # => 1