John N. wrote in post #960298:
> Hello everyone,
>
> Let's say I have the following set of data in my database table:
>
> Cars table
>
> -------------------------------------
> [Brand]      [Type]
> Mercedes     CL63
> BMW          M3
> Mercedes     CL63
> Audi         RS4
> BMW          M3
> BMW          M3
> -------------------------------------
>
> I would like to know how many cartypes I have of a particular brand.
> So in this case I would expect an output something like:
>
> 2x Mercedes CL63
> 3x BMW M3
> 1x Audi RS4
>
> 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.

Once you know about aggregate functions, look at the 
ActiveRecord::Calculations module (at least in Rails 2; it might have 
got moved in Rails 3), which abstracts these functions and provides a 
nice AR interface.

> Suggestions, tips that can pull me in the right direction are welcome.
>

My usual tip: learn SQL.  You may not have to write much of it when 
using Rails, but you need to know how SQL databases basically work.  AR 
exists to automate and abstract, not to prevent you from having to learn 
SQL.

> Thanks a bunch.
>
> John

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