Calling .values.sum might have to load a LOT of (id, count) rows so it isn't a 
very scalable solution.

Calling regroup or something is actually changing what would be counted - the 
group can affect the count, especially if you use HAVING clauses.

There's nothing wrong with having the grouped count functionality, but it's 
messing up the #count API IMHO.


On 13/09/2014, at 03:40 , Gabriel Sobrinho <gabriel.sobri...@gmail.com> wrote:

> I'm against to count and size works equally since I would want to know how 
> many grouped records do I have (i.e.: how many roles do I have on determining 
> relation).
> 
> I think that count and size is working as both should.
> 
> 
> Although, if you want to count all records of a grouped relation, you have to 
> remove the group or just sum the counts:
> 
> User.count
> 
> or: 
>  
> User.group(:role).count.values.sum
> 
> Not elegant but if you can't remove the group for that count, you have to do 
> that.
> 
> 
> One possible option would rails have something like regroup(false) as 
> reorder(false), but again I think that just adding the values is more easier 
> for everyone :)
> 
> On Thursday, September 11, 2014 11:54:14 PM UTC-3, Shinohara Teruki 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.
> 
> 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> 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.
> To post to this group, send email to rubyonra...@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.

Reply via email to