aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorDaniel E. Garcia Shulman <vinagrito@users.noreply.github.com>2016-11-02 21:45:54 +0200
committerSean Griffin <sean@seantheprogrammer.com>2016-11-02 15:45:54 -0400
commit032ab94332fd5496c5af627bc36445cea3ba476a (patch)
treed05d12ef989a1bcee943da587227b8b0b72c8c42 /activerecord
parent7b0b119c500d45b9bc751e5346ad5f316dddcc29 (diff)
downloadrails-032ab94332fd5496c5af627bc36445cea3ba476a.tar.gz
rails-032ab94332fd5496c5af627bc36445cea3ba476a.tar.bz2
rails-032ab94332fd5496c5af627bc36445cea3ba476a.zip
Fixes TypeError Exception when cache counter value equals nil (#26940)
* Fixes TypeError when cache counter value equals nil * Test case for counter cache on unloaded has_many association
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/associations/has_many_association.rb2
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb5
2 files changed, 6 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb
index d1d0cc4c49..742cd25509 100644
--- a/activerecord/lib/active_record/associations/has_many_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_association.rb
@@ -72,7 +72,7 @@ module ActiveRecord
# the loaded flag is set to true as well.
def count_records
count = if reflection.has_cached_counter?
- owner._read_attribute reflection.counter_cache_column
+ owner._read_attribute(reflection.counter_cache_column).to_i
else
scope.count
end
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index 7eb04b1ffd..3afd062950 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -1742,6 +1742,11 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert !company.clients.loaded?
end
+ def test_counter_cache_on_unloaded_association
+ car = Car.create(name: "My AppliCar")
+ assert_equal car.engines.size, 0
+ end
+
def test_get_ids_ignores_include_option
assert_equal [readers(:michael_welcome).id], posts(:welcome).readers_with_person_ids
end