Forum: Ruby on Rails Ad hoc query from derived view

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
dsliwa (Guest)
on 2006-04-11 04:56
(Received via mailing list)
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)))
Alain R. (Guest)
on 2006-04-11 18:33
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/HasManyAssociationsTe...

Alain
This topic is locked and can not be replied to.