# Find\_by\_sql suggestions needed

**URL:** <https://www.ruby-forum.com/t/find-by-sql-suggestions-needed/149518>\
**Category:** Rails\
**Created:** [September 25, 2008, 6:23pm UTC](https://www.ruby-forum.com/t/find-by-sql-suggestions-needed/149518 "2008-09-25T18:23:39Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![cnjohnson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/57b2e6/32.png) [@cnjohnson](https://www.ruby-forum.com/u/cnjohnson)\
**Post date:** [September 25, 2008, 6:23pm UTC](https://www.ruby-forum.com/t/find-by-sql-suggestions-needed/149518/1 "2008-09-25T18:23:39Z")

</div>

I have an application that generates an ever growing list of stock  
reports. I find myself some method in a controller creating a  
connection to the database (postgres in this case), executing a custom  
sql, using the data returned to create one or more graphs with gruff,  
and sending them off to be displayed.

This puts the creation of a connection to the database, and the actual  
sql in the controller. I am not sure that this is such a good idea. I  
am looking for suggestions on a better approach.

Should I create a separate class with a method to match each arbitrary  
sql returning the data from a method call?

Should the queries be converted to stored functions which then get  
called from the controller, or a separate class?

Insights would be appreciated.

Cheers–

Charles
