diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2016-09-04 07:12:39 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2016-09-04 07:27:55 +0900 |
commit | 8ad03c97da0c84b9e96587ea22e9a4a0e107b3c1 (patch) | |
tree | 44d7f7a67772bc8f695dbbcbe4994776908c7960 /activerecord/lib | |
parent | ce97dc1abf4d301c8a4aaa7e790088b8d318afd4 (diff) | |
download | rails-8ad03c97da0c84b9e96587ea22e9a4a0e107b3c1.tar.gz rails-8ad03c97da0c84b9e96587ea22e9a4a0e107b3c1.tar.bz2 rails-8ad03c97da0c84b9e96587ea22e9a4a0e107b3c1.zip |
Remove unnecessary `count` method for collection proxy
Simply use its own method because `CollectionProxy` inherits `Relation`.
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/collection_association.rb | 25 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/collection_proxy.rb | 25 |
2 files changed, 14 insertions, 36 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index 5ee6960c86..d344277ab2 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -183,31 +183,6 @@ module ActiveRecord end end - # Returns the number of records. If no arguments are given, it counts all - # columns using SQL. If one argument is given, it counts only the passed - # column using SQL. If a block is given, it counts the number of records - # yielding a true value. - def count(column_name = nil) - return super if block_given? - relation = scope - if association_scope.distinct_value - # This is needed because 'SELECT count(DISTINCT *)..' is not valid SQL. - column_name ||= reflection.klass.primary_key - relation = relation.distinct - end - - value = relation.count(column_name) - - limit = options[:limit] - offset = options[:offset] - - if limit || offset - [ [value - offset.to_i, 0].max, limit.to_i ].min - else - value - end - end - # Removes +records+ from this association calling +before_remove+ and # +after_remove+ callbacks. # diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index 48436155ce..dda240585e 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -743,6 +743,20 @@ module ActiveRecord end alias uniq distinct + def calculate(operation, column_name) + null_scope? ? scope.calculate(operation, column_name) : super + end + + def pluck(*column_names) + null_scope? ? scope.pluck(*column_names) : super + end + + ## + # :method: count + # + # :call-seq: + # count(column_name = nil, &block) + # # Count all records. # # class Person < ActiveRecord::Base @@ -762,17 +776,6 @@ module ActiveRecord # perform the count using Ruby. # # person.pets.count { |pet| pet.name.include?('-') } # => 2 - def count(column_name = nil, &block) - @association.count(column_name, &block) - end - - def calculate(operation, column_name) - null_scope? ? scope.calculate(operation, column_name) : super - end - - def pluck(*column_names) - null_scope? ? scope.pluck(*column_names) : super - end # Returns the size of the collection. If the collection hasn't been loaded, # it executes a <tt>SELECT COUNT(*)</tt> query. Else it calls <tt>collection.size</tt>. |