Michael Pavling wrote in post #960375:
[...]
> On 9 November 2010 13:37, Marnen Laibow-Koser <[email protected]>
> wrote:
>>> How can I generate such an output? I have really no clue how to start.
>>
>> You would do this by using SQL aggregate functions (in this case,
>> COUNT(*) ). If you don't know about those, then your friendly local SQL
>> reference is your first stop.
>
> In addition to using the SQL functions, Ruby and Rails both include
> functions for grouping (and sort, if you wish) enumerable objects.
> Have a look at:
> http://api.rubyonrails.org/classes/Enumerable.html#method-i-group_by
> http://ruby-doc.org/core/classes/Enumerable.html
>
> The choice as to whether to do the same thing in code or in the DB is
> down to you, and your evaluation of the efficiency/benefit one may
> give you over the other (personal preference comes into it a lot too!
> :-)

However, here's a rule of thumb: *never* use Ruby for things like a 
simple count of DB records.  Let the DB do the things that it's good at. 
That includes mass operations on large amounts of data.  Complex 
calculations and application programming are generally better done in 
the app layer, of course.

Doing a count of DB records on the Ruby side?  I wouldn't dignify that 
with the term "personal preference".

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
[email protected]

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to