Module: PreloadCounts
- Defined in:
- lib/preload_counts/ar.rb,
lib/preload_counts/version.rb
Overview
This adds a scope to preload the counts of an association in one SQL query.
Consider the following code: Service.all.each{|s| puts s.incidents.acknowledged.count}
Each time count is called, a db query is made to fetch the count.
Adding this to the Service class:
preload_counts :incidents => [:acknowledged]
will add a preload_incident_counts scope to preload the counts and add accessors to the class. So our codes becaumes
Service.preload_incident_counts.all.each{|s| puts s.acknowledged_incidents_count}
And only requires one DB query.
Defined Under Namespace
Modules: ClassMethods, InstanceMethods
Constant Summary collapse
- VERSION =
"0.0.4"
Class Method Summary collapse
Class Method Details
.included(receiver) ⇒ Object
92 93 94 95 |
# File 'lib/preload_counts/ar.rb', line 92 def self.included(receiver) receiver.extend ClassMethods receiver.send :include, InstanceMethods end |