# Design patterns

**URL:** <https://www.ruby-forum.com/t/design-patterns/105002>\
**Category:** Rails\
**Created:** [May 24, 2007, 8:19pm UTC](https://www.ruby-forum.com/t/design-patterns/105002 "2007-05-24T20:19:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mguzman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bb73d2/32.png) [@mguzman](https://www.ruby-forum.com/u/mguzman)\
**Post date:** [May 24, 2007, 8:19pm UTC](https://www.ruby-forum.com/t/design-patterns/105002/1 "2007-05-24T20:19:55Z")

</div>

Hi everyone  
I’m Maximiliano G. from Brazil and I am programing on ruby on  
rails  
only a couple of months.  
I read a lot of books and articles about it but I could not clarify some  
doubts about design patterns:

1. Is it really good put some sql command on controller? because all  
articles that I see use sql into controllers, like:

2. In my app I have some reports, like that:  
#report controller  
sql = \<\<EOF  
select categories.id, categories.name, sum(ledgers.value), sum(  
budgets.value)  
from categories, ledgers, budgets  
where categories.id = ledgers.category\_id  
and categories.id = budgets.category\_id  
group by categories.id, categories.name  
EOF

cheers,

–  
Atenciosamente,

Maximiliano Ferreira Guzenski  
Sun Certified Java Programmer 1.4  
Sun Certified Web Component Developer, Enterprise Edition 1.4

[http://www.guzenski.com.br](http://www.guzenski.com.br)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![mguzman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bb73d2/32.png) [@mguzman](https://www.ruby-forum.com/u/mguzman)\
**Post date:** [May 24, 2007, 9:06pm UTC](https://www.ruby-forum.com/t/design-patterns/105002/2 "2007-05-24T21:06:07Z")

</div>

1. It’s all about agile development. If you find yourself using exactly  
that  
string, or very close to it, then look into moving it from the  
controller  
into the model. Make sure you have tests in place to verify that you  
didn’t  
break anything through this refactoring.

2. For specific hard-coded SQL, probably best to put it in the model.  
Also,  
Model.find\_by\_sql is perfectly fine.

Hope that helps. Welcome to Rails!

Jason

---

<div class="post-metadata">

**Author:** ![mguzman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bb73d2/32.png) [@mguzman](https://www.ruby-forum.com/u/mguzman)\
**Post date:** [May 26, 2007, 5:11am UTC](https://www.ruby-forum.com/t/design-patterns/105002/3 "2007-05-26T05:11:18Z")

</div>

Hi Maximiliano,

If you want to be strict about design patterns, then you want to keep  
database queries and related find\_by methods in the Model layer of  
the MVC framework.

Also, that’s what the “Agile Development with Rails” book recommend.  
Just a little extra point, you might want to use a more descriptive  
name for the “sql” variable. It’s all about readability.

Cheers,  
Mauricio

=== I’m brazilian, so here’s the same thing in Portuguese 😉 ====

Se vocÃª tÃ¡ estudando design patterns e quer seguir os padrÃµes, entÃ£o  
mantenha todos os mÃ©todos e buscas relacionados ao banco de dados na  
camada de Modelo do padrÃ£o MVC.

O livro “Agile Development with Rails” tambÃ©m recomenda isso. AlÃ©m  
disso, eu recomendaria que vocÃª usasse um nome diferente pra sua  
variÃ¡vel de busca, algo mais descritivo do que “sql”, pra facilitar a  
releitura desse cÃ³digo.

AbraÃ§Ã£o,  
Mauricio

* * *

Mauricio L. Sadicoff  
removed\_email\_address@domain.invalid

“Peace of mind isn’t at all superficial, really,” I expound. “It’s  
the whole thing. That which produces it is good maintenance; that  
which disturbs it is poor maintenance. What we call workability of  
the machine is just an objectification of this peace of mind. The  
ultimate test is always your own serenity.” - Zen and the Art of  
Motorcycle Maintenance.

---

<div class="post-metadata">

**Author:** ![mguzman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bb73d2/32.png) [@mguzman](https://www.ruby-forum.com/u/mguzman)\
**Post date:** [May 29, 2007, 11:05am UTC](https://www.ruby-forum.com/t/design-patterns/105002/4 "2007-05-29T11:05:21Z")

</div>

If your web application data is tied yo your web application, then you  
could probably just have all your SQL in your model.

If you want to use the same data from outside of the web application,  
then your model should be totally outside of Rails and just have a thin  
model to your ‘real’ model.

The controller, in general, shouldn’t really know anything about the  
underlying model. It just, in general, controls flow.

Also, MVC isn’t really a pattern, it;s a compound pattern. It’s made up  
of other patterns. So, if you’re model is outside of Rails, you could  
use an adapter from your thin model to your real model.

I hope this helps but as with everything, there’s more than one way to  
do it, and everyone will have their angle on how MVC works. What’s good  
for me, might not be good for you.

-Ants

Maximiliano G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote: Hi everyone  
I’m Maximiliano G. from Brazil and I am programing on ruby on  
rails only a couple of months.  
I read a lot of books and articles about it but I could not clarify some  
doubts about design patterns:

1. Is it really good put some sql command on controller? because all  
articles that I see use sql into controllers, like:

2. In my app I have some reports, like that:  
#report controller  
sql = \<\<EOF  
select categories.id, categories.name , sum(ledgers.value),  
sum(budgets.value)  
from categories, ledgers, budgets  
where categories.id = ledgers.category\_id  
and categories.id = budgets.category\_id  
group by categories.id, categories.name  
EOF

cheers,

–  
Atenciosamente,

Maximiliano Ferreira Guzenski  
Sun Certified Java Programmer 1.4  
Sun Certified Web Component Developer, Enterprise Edition 1.4

[http://www.guzenski.com.br](http://www.guzenski.com.br)  
removed\_email\_address@domain.invalid

* * *

Now you can scan emails quickly with a reading pane. Get the new Yahoo!  
Mail.

---

<div class="post-metadata">

**Author:** ![mguzman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bb73d2/32.png) [@mguzman](https://www.ruby-forum.com/u/mguzman)\
**Post date:** [May 29, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/design-patterns/105002/5 "2007-05-29T18:11:56Z")

</div>

just a comment on #1, +1 to everybody’s suggestion of keeping it in  
the model. check out “Skinny Controller, Fat Model”:  
[http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model](http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model)
