Ad hoc query from derived view


#1

I’m new to rails and have a query that uses a derived view (see below)
and
am wondering how return the data. I don’t have a model for this, so I
not
sure if find_by_sql can be used. Any help is greatly appreciated.

              select product
                      ,mailer
                      ,sum(dev_total) dev
                      ,sum(qa_total) qa
                      ,sum(verified_total) verified
                 from (select product
                              ,mailer
                              ,count(1) dev_total
                              ,0 qa_total
                              ,0 verified_total
                         from (select case
                                       when short_desc like '%prod%'

then
‘Production’
else
email_product
end product
,case
when version = ‘prod’ and
priority = ‘P4’ then
‘PCR’
when version = ‘prod’ and
priority != ‘P4’ then
‘BUG’
else
mailer
end mailer
,case
when bug_status in
(‘new’,‘assigned’) then
‘DEV’
when bug_status in
(‘VERIFIED’,‘CLOSED’) then
‘VERIFIED’
when resolution in (‘Fixed’)
then
‘QA’
else
‘?’
end status
from mailer_bugs_v a
where status_whiteboard_number =
3.49)
a
where status = ‘DEV’
group by product
,mailer
,status)))


#2

dsliwa

              select product ,mailer ,sum(dev_total) ...
                         group by product ...

I’ve never used it, but modern Rails offers grouping and calculations
at the model level (without resorting to pure sql) :
See:
http://railsmanual.org/class/HasManyAssociationsTest/test_find_grouped

Alain