>> size in this case is an pretty much an "alias" for count, thus it's gonna work the same way as count when grouping.
I think that current #size don't behave like an alias for #count. g = User.group(:role); g.load; g.count => {:normal=>3, :admin=>4} g = User.group(:role); g.load; g.size => 2 When I call #load before #count or #size, a return value of #count don't equals to a return value of #size. >> Is there any particular reason to know how many records returned if you are grouping? I expect #count and #size to return same value. It is not the case that I want to know how many records the grouping query returns. My ideal implementation: g = User.group(:role); g.load; g.count => {:normal=>3, :admin=>4} g = User.group(:role); g.load; g.size =>{:normal=>3, :admin=>4} or g = User.group(:role); g.load; g.count => 2 g = User.group(:role); g.load; g.size =>2 What do you think? Thank you 2014年9月12日金曜日 8時21分39秒 UTC+9 Carlos Antonio da Silva: > > 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....@gmail.com > <javascript:>> 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-co...@googlegroups.com <javascript:>. >> To post to this group, send email to rubyonra...@googlegroups.com >> <javascript:>. >> 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.