# Complex sql query approach

**URL:** <https://www.ruby-forum.com/t/complex-sql-query-approach/82652>\
**Category:** Rails\
**Created:** [January 19, 2007, 4:32pm UTC](https://www.ruby-forum.com/t/complex-sql-query-approach/82652 "2007-01-19T16:32:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sean\_OSHara](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@Sean\_OSHara](https://www.ruby-forum.com/u/Sean_OSHara)\
**Post date:** [January 19, 2007, 4:32pm UTC](https://www.ruby-forum.com/t/complex-sql-query-approach/82652/1 "2007-01-19T16:32:01Z")

</div>

Hi All,

I have a question about what would be the best or the “rails” way to  
generate a report from my web app that is basically a web store. I  
wonder if  
I should just come up with a complex sql query by hand (which would  
probably  
take a while to figure out since I don’t know much beyond basic sql,  
i.e.  
haven’t delved into joins yet). Or should I just use my rails models and  
attributes and do all the calculations in ruby (I fear this could this  
could  
entail a significant database hit).

Here is my data structure.

Artists has\_many Releases has\_many Products has\_many LineItems has\_many  
Orders.

I would like to generate a sales report that lists each artist with  
their  
associated releases and for each product associated with that release I  
would like to list the number of copies sold and the total sales price.

Any opinions on the best way to approach this?

Thanks in advance,  
Sean

---

<div class="post-metadata">

**Author:** ![Sean\_OSHara](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/dbc845/32.png) [@Sean\_OSHara](https://www.ruby-forum.com/u/Sean_OSHara)\
**Post date:** [September 25, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/complex-sql-query-approach/82652/2 "2007-09-25T22:40:32Z")

</div>

On Thu, 2007-01-11 at 19:39 -0500, Sean O’Hara wrote:

> Any opinions on the best way to approach this?

* * *

the find (complex searches)…

[http://brainspl.at/articles/2006/06/30/new-release-of-ez\_where-plugin](http://brainspl.at/articles/2006/06/30/new-release-of-ez_where-plugin)

the view code - start hacking

Craig
