diff options
author | Ryan Garver <ragarver@gmail.com> | 2012-10-04 08:13:12 -0700 |
---|---|---|
committer | Ryan Garver <ragarver@gmail.com> | 2012-10-04 08:13:12 -0700 |
commit | 1f3a8c873ed9b3fc8eb41ae775a570b95ebdf715 (patch) | |
tree | 7dad02bf5f727d327fae9ccac1c2007dc4641be1 /activerecord/lib | |
parent | 4cb50a3f571234b1202f9a0dffe39b445ecf807d (diff) | |
parent | d7d228402efd1ddbbdb2b15d2e91ec22693b9298 (diff) | |
download | rails-1f3a8c873ed9b3fc8eb41ae775a570b95ebdf715.tar.gz rails-1f3a8c873ed9b3fc8eb41ae775a570b95ebdf715.tar.bz2 rails-1f3a8c873ed9b3fc8eb41ae775a570b95ebdf715.zip |
Merge branch 'master' into feature/public-fragment_name_with_digest
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/collection_association.rb | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/counter_cache.rb | 9 |
2 files changed, 6 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index fe3e5b00f7..96270ec0e9 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -174,6 +174,8 @@ module ActiveRecord # association, it will be used for the query. Otherwise, construct options and pass them with # scope to the target class's +count+. def count(column_name = nil, count_options = {}) + return 0 if owner.new_record? + column_name, count_options = nil, column_name if column_name.is_a?(Hash) if options[:counter_sql] || options[:finder_sql] diff --git a/activerecord/lib/active_record/counter_cache.rb b/activerecord/lib/active_record/counter_cache.rb index d28cd560d9..57838ff984 100644 --- a/activerecord/lib/active_record/counter_cache.rb +++ b/activerecord/lib/active_record/counter_cache.rb @@ -23,12 +23,11 @@ module ActiveRecord has_many_association = reflect_on_association(association.to_sym) if has_many_association.is_a? ActiveRecord::Reflection::ThroughReflection - foreign_key = has_many_association.through_reflection.foreign_key.to_s - child_class = has_many_association.through_reflection.klass - else - foreign_key = has_many_association.foreign_key.to_s - child_class = has_many_association.klass + has_many_association = has_many_association.through_reflection end + + foreign_key = has_many_association.foreign_key.to_s + child_class = has_many_association.klass belongs_to = child_class.reflect_on_all_associations(:belongs_to) reflection = belongs_to.find { |e| e.foreign_key.to_s == foreign_key && e.options[:counter_cache].present? } counter_name = reflection.counter_cache_column |