diff options
author | Yves Senn <yves.senn@gmail.com> | 2012-11-04 17:09:25 +0100 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2012-11-04 17:09:25 +0100 |
commit | 710e3710813e52ce71ee2cd5a754cf6462b2ecd3 (patch) | |
tree | 4622f9356af6e19830920c2f875f122194655deb /activerecord/lib/active_record | |
parent | f786469f54332458ed60a1bbefec3125afe5f64b (diff) | |
download | rails-710e3710813e52ce71ee2cd5a754cf6462b2ecd3.tar.gz rails-710e3710813e52ce71ee2cd5a754cf6462b2ecd3.tar.bz2 rails-710e3710813e52ce71ee2cd5a754cf6462b2ecd3.zip |
:counter_cache option for to support custom named counter caches. Closes #7993
Diffstat (limited to 'activerecord/lib/active_record')
3 files changed, 5 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 69b95f814c..5dd833e5a6 100644 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1122,6 +1122,9 @@ module ActiveRecord # If using with the <tt>:through</tt> option, the association on the join model must be # a +belongs_to+, and the records which get deleted are the join records, rather than # the associated records. + # [:counter_cache] + # This option can be used to configure a custom named <tt>:counter_cache.</tt> You only need this option, + # when you customized the name of your <tt>:counter_cache</tt> on the <tt>belongs_to</tt> association. # [:as] # Specifies a polymorphic interface (See <tt>belongs_to</tt>). # [:through] diff --git a/activerecord/lib/active_record/associations/builder/has_many.rb b/activerecord/lib/active_record/associations/builder/has_many.rb index ab8225460a..0d1bdd21ee 100644 --- a/activerecord/lib/active_record/associations/builder/has_many.rb +++ b/activerecord/lib/active_record/associations/builder/has_many.rb @@ -5,7 +5,7 @@ module ActiveRecord::Associations::Builder end def valid_options - super + [:primary_key, :dependent, :as, :through, :source, :source_type, :inverse_of] + super + [:primary_key, :dependent, :as, :through, :source, :source_type, :inverse_of, :counter_cache] end def valid_dependent_options diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index 74864d271f..f59565ae77 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -76,7 +76,7 @@ module ActiveRecord end def cached_counter_attribute_name(reflection = reflection) - "#{reflection.name}_count" + options[:counter_cache] || "#{reflection.name}_count" end def update_counter(difference, reflection = reflection) |