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:
parent
7b0b119c50
commit
032ab94332
2 changed files with 6 additions and 1 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue