Pagination. Comes up all the time for us - we have a query which paginates through table A, and we want to also grab the total of table B records owned by each A, so we have a joins().group().select(), maybe even a having(). Then count() comes along and runs our party by returning a hash instead of using a subquery to count the number of results that would returned by the original scope.
Two separate things have been conflated for historical reasons - counting results which may use grouping themselves, and getting grouped counts. So I don't think we should fix #size, I think we should fix #count. IMHO there should be a #grouped_count or something to get the current behavior, and #count should always find the count of things in the relation. On 12/09/2014, at 11:20 , Carlos Antonio da Silva <carlosantoniodasi...@gmail.com> wrote: > size in this case is an pretty much an "alias" for count, thus it's gonna > work the same way as count when grouping. > > Is there any particular reason to know how many records returned if you are > grouping? > > On Wed, Sep 10, 2014 at 11:16 AM, Shinohara Teruki <ts.3...@gmail.com> wrote: > ActiveRecord::Relation.size returns a Hash when I use a group method. > I think that a size method is expected to return integer typically. > > I propose this implementation: > > def size > if loaded? > @records.length > else > count_value = count(:all) > count_value.is_a?(Hash) ? count_value.length : count_value > end > end > > This is a current implementation: > > # > https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation.rb > # Returns size of the records. > def size > loaded? ? @records.length : count(:all) > end > > > A count method is implemented on calculations.rb. > https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation/calculations.rb > > > What do you think? > > Thank you > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Core" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-core+unsubscr...@googlegroups.com. > To post to this group, send email to rubyonrails-core@googlegroups.com. > Visit this group at http://groups.google.com/group/rubyonrails-core. > For more options, visit https://groups.google.com/d/optout. > > > > -- > At. > Carlos Antonio > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Core" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-core+unsubscr...@googlegroups.com. > To post to this group, send email to rubyonrails-core@googlegroups.com. > Visit this group at http://groups.google.com/group/rubyonrails-core. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-core+unsubscr...@googlegroups.com. To post to this group, send email to rubyonrails-core@googlegroups.com. Visit this group at http://groups.google.com/group/rubyonrails-core. For more options, visit https://groups.google.com/d/optout.