aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorJamis Buck <jamis@37signals.com>2006-03-09 17:23:57 +0000
committerJamis Buck <jamis@37signals.com>2006-03-09 17:23:57 +0000
commit87898badba211540d9c1d4dae0cd513a9d525554 (patch)
tree5423e8e10a2b0d9d48b845230d33028315e2d794 /activerecord/lib
parent130001c377e7abf17ea2f4f856cab30882a60f92 (diff)
downloadrails-87898badba211540d9c1d4dae0cd513a9d525554.tar.gz
rails-87898badba211540d9c1d4dae0cd513a9d525554.tar.bz2
rails-87898badba211540d9c1d4dae0cd513a9d525554.zip
Allow counter_cache to accept a column name
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3825 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib')
-rwxr-xr-xactiverecord/lib/active_record/associations.rb11
1 files changed, 8 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index b25df26059..a098c0f326 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -460,7 +460,8 @@ module ActiveRecord
# * <tt>:counter_cache</tt> - caches the number of belonging objects on the associate class through use of increment_counter
# and decrement_counter. The counter cache is incremented when an object of this class is created and decremented when it's
# destroyed. This requires that a column named "#{table_name}_count" (such as comments_count for a belonging Comment class)
- # is used on the associate class (such as a Post class).
+ # is used on the associate class (such as a Post class). You can also specify a custom counter cache column by given that
+ # name instead of a true/false value to this option (e.g., <tt>:counter_cache => :my_custom_counter</tt>.)
# * <tt>:include</tt> - specify second-order associations that should be eager loaded when this object is loaded.
#
# Option examples:
@@ -515,13 +516,17 @@ module ActiveRecord
end
if options[:counter_cache]
+ cache_column = options[:counter_cache] == true ?
+ "#{self.to_s.underscore.pluralize}_count" :
+ options[:counter_cache]
+
module_eval(
- "after_create '#{reflection.name}.class.increment_counter(\"#{self.to_s.underscore.pluralize + "_count"}\", #{reflection.primary_key_name})" +
+ "after_create '#{reflection.name}.class.increment_counter(\"#{cache_column}\", #{reflection.primary_key_name})" +
" unless #{reflection.name}.nil?'"
)
module_eval(
- "before_destroy '#{reflection.name}.class.decrement_counter(\"#{self.to_s.underscore.pluralize + "_count"}\", #{reflection.primary_key_name})" +
+ "before_destroy '#{reflection.name}.class.decrement_counter(\"#{cache_column}\", #{reflection.primary_key_name})" +
" unless #{reflection.name}.nil?'"
)
end