diff options
author | Klas Eskilson <klas.eskilson@gmail.com> | 2017-01-03 16:24:42 -0800 |
---|---|---|
committer | Klas Eskilson <klas.eskilson@gmail.com> | 2017-02-07 11:09:34 -0800 |
commit | 4a7b4f88cd3fc10ab56edc3f88d5db0c4f871dc9 (patch) | |
tree | 55393f0cecba8fb49b28585502d7bea653129963 /activerecord/lib/active_record | |
parent | 6599e07674d2389de45b91a11d3be5eb0fbd92f3 (diff) | |
download | rails-4a7b4f88cd3fc10ab56edc3f88d5db0c4f871dc9.tar.gz rails-4a7b4f88cd3fc10ab56edc3f88d5db0c4f871dc9.tar.bz2 rails-4a7b4f88cd3fc10ab56edc3f88d5db0c4f871dc9.zip |
Use `count(:all)` in HasManyAssociation#count_records
Problem: Calling `count` on an association can cause invalid SQL queries
to be created where the `SELECT COUNT(a, b, c)` function receives
multiple columns. This will cause a `StatementInvalid` exception later
on.
Solution: Use `count(:all)`, which generates a `SELECT COUNT(*)...`
query independently of the association.
This also includes a test case that, before the fix, broke.
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/associations/has_many_association.rb | 2 |
1 files changed, 1 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 b413eb3f9c..827e38ab4a 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -66,7 +66,7 @@ module ActiveRecord count = if reflection.has_cached_counter? owner._read_attribute(reflection.counter_cache_column).to_i else - scope.count + scope.count(:all) end # If there's nothing in the database and @target has no new records |