1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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
This commit is contained in:
Daniel E. Garcia Shulman 2016-11-02 21:45:54 +02:00 committed by Sean Griffin
parent 7b0b119c50
commit 032ab94332
2 changed files with 6 additions and 1 deletions

View file

@ -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

View file

@ -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