# MVC architecture of ROR

**URL:** <https://www.ruby-forum.com/t/mvc-architecture-of-ror/145709>\
**Category:** Rails\
**Created:** [August 13, 2008, 7:16am UTC](https://www.ruby-forum.com/t/mvc-architecture-of-ror/145709 "2008-08-13T07:16:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [August 13, 2008, 7:16am UTC](https://www.ruby-forum.com/t/mvc-architecture-of-ror/145709/1 "2008-08-13T07:16:57Z")

</div>

Hi,  
As the ROR follows MVC architecture, then Is it good practice to  
write queries inside controller?  
If yes then, For what purpose we are using model?

Thanks & Regards,  
Tushar

---

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [August 13, 2008, 7:21am UTC](https://www.ruby-forum.com/t/mvc-architecture-of-ror/145709/2 "2008-08-13T07:21:13Z")

</div>

> As the ROR follows MVC architecture, then Is it good practice to  
> write queries inside controller?  
> If yes then, For what purpose we are using model?

I’ve always tried to put as much of the query logic into the model  
itself (google for fat model skinny controller).

I find I use named scopes a lot now so that in my controllers I can do  
things like:

@posts = Post.approved.recent

and keep things nice and readable and my model would look something  
like:

named\_scope :approved, :conditions =\> {:is\_approved =\>  
true}, :order =\> ‘created\_at DESC’  
named\_scope :recent, :order =\> ‘created\_at DESC’, :limit =\> 5

The duplicated :order’s is because I might not necessarily chain the  
scopes…

-philip

---

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [August 13, 2008, 7:26am UTC](https://www.ruby-forum.com/t/mvc-architecture-of-ror/145709/3 "2008-08-13T07:26:34Z")

</div>

Philip H. wrote:

> > As the ROR follows MVC architecture, then Is it good practice to  
> > write queries inside controller?  
> > If yes then, For what purpose we are using model?
> 
> I’ve always tried to put as much of the query logic into the model  
> itself (google for fat model skinny controller).
> 
> I find I use named scopes a lot now so that in my controllers I can do  
> things like:
> 
> @posts = Post.approved.recent
> 
> and keep things nice and readable and my model would look something  
> like:
> 
> named\_scope :approved, :conditions =\> {:is\_approved =\>  
> true}, :order =\> ‘created\_at DESC’  
> named\_scope :recent, :order =\> ‘created\_at DESC’, :limit =\> 5
> 
> The duplicated :order’s is because I might not necessarily chain the  
> scopes…
> 
> -philip  
> HI,  
> I am agree with this that, our model will look like this:  
> named\_scope :approved, :conditions =\> {:is\_approved =\>  
> true}, :order =\> ‘created\_at DESC’

But, each time we are not able to find the records by using modelling at  
all. Sometimes we have to write the complex query for 2 or 3 tables,  
then such type of queries where should I write?  
Thanks,  
Tushar

---

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [August 13, 2008, 7:35am UTC](https://www.ruby-forum.com/t/mvc-architecture-of-ror/145709/4 "2008-08-13T07:35:16Z")

</div>

> > scopes…  
> > then such type of queries where should I write?  
> > Good point. Well, if it’s the complexity is due to the nature of the  
> > action of the controller (some advanced search form say) then putting  
> > it in the controller makes sense as you’d never call it from anywhere  
> > else.

I’m partly guessing here since I don’t know your app, but putting it  
in the controller makes sense in that case.

But if you find yourself making that same query in a variety of places  
it should find it’s way into a model somewhere… just for code reuse  
at a minimum.

-philip

---

<div class="post-metadata">

**Author:** ![gandhi-tush](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@gandhi-tush](https://www.ruby-forum.com/u/gandhi-tush)\
**Post date:** [August 13, 2008, 11:34am UTC](https://www.ruby-forum.com/t/mvc-architecture-of-ror/145709/5 "2008-08-13T11:34:51Z")

</div>

Hi,

Jamis B. nailed it in his post:  
[Buckblog: Skinny Controller, Fat Model](http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model).  
Kind regards  
Nicolai
