# Put code in model or controller. Any general rules of thumb?

**URL:** <https://www.ruby-forum.com/t/put-code-in-model-or-controller-any-general-rules-of-thumb/81464>\
**Category:** Rails\
**Created:** [January 9, 2007, 3:59am UTC](https://www.ruby-forum.com/t/put-code-in-model-or-controller-any-general-rules-of-thumb/81464 "2007-01-09T03:59:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pervel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7bcc69/32.png) [@pervel](https://www.ruby-forum.com/u/pervel)\
**Post date:** [January 9, 2007, 3:59am UTC](https://www.ruby-forum.com/t/put-code-in-model-or-controller-any-general-rules-of-thumb/81464/1 "2007-01-09T03:59:21Z")

</div>

I am a Rails newbie (but far from a programming newbie). I have taken  
perhaps a strange path to learning Rails. I started getting experience  
with Ruby - lovely language indeed! Then I ventured into ActiveRecord.  
Slick and easy to understand framework.

Now, I am starting on the controllers. But that gives me some doubts. I  
am constantly wondering if I have bloated my models with too much code.  
So I would like to ask if there is perhaps a write-up somewhere about  
what to consider when deciding between putting code in the model or in  
the controller?

When I get to learning about the views, I will probably wonder about  
the same issue. I understand that the rule of thumb here is basically  
to put as little code in the view as possible. So again that leads back  
to the question of putting the code to support the views in the model  
or the controller.

I know these are pretty general questions. And I am certainly not  
looking for a “fact book”. Just some insight into how other people tend  
to make their decisions about where to put the code. 🙂

---

<div class="post-metadata">

**Author:** ![pervel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7bcc69/32.png) [@pervel](https://www.ruby-forum.com/u/pervel)\
**Post date:** [January 9, 2007, 4:25am UTC](https://www.ruby-forum.com/t/put-code-in-model-or-controller-any-general-rules-of-thumb/81464/2 "2007-01-09T04:25:03Z")

</div>

Per V. wrote:

> I am a Rails newbie (but far from a programming newbie). I have taken  
> perhaps a strange path to learning Rails. I started getting experience  
> with Ruby - lovely language indeed! Then I ventured into ActiveRecord.  
> Slick and easy to understand framework.
> 
> Now, I am starting on the controllers. But that gives me some doubts. I  
> am constantly wondering if I have bloated my models with too much code.  
> So I would like to ask if there is perhaps a write-up somewhere about  
> what to consider when deciding between putting code in the model or in  
> the controller?
> 
> When I get to learning about the views, I will probably wonder about  
> the same issue. I understand that the rule of thumb here is basically  
> to put as little code in the view as possible. So again that leads back  
> to the question of putting the code to support the views in the model  
> or the controller.
> 
> I know these are pretty general questions. And I am certainly not  
> looking for a “fact book”. Just some insight into how other people tend  
> to make their decisions about where to put the code. 🙂

This helped me :  
[http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model](http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model)

---

<div class="post-metadata">

**Author:** ![pervel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7bcc69/32.png) [@pervel](https://www.ruby-forum.com/u/pervel)\
**Post date:** [January 9, 2007, 6:52am UTC](https://www.ruby-forum.com/t/put-code-in-model-or-controller-any-general-rules-of-thumb/81464/3 "2007-01-09T06:52:32Z")

</div>

On 1/3/07, Per V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am a Rails newbie (but far from a programming newbie).

So am I so take the rest of the mail with a pinch of salt. 🙂

> So again that leads back  
> to the question of putting the code to support the views in the model  
> or the controller.

when I started my first RoR app, views were pretty fat, after my first  
iteration I moved most of the code to controller, now in my second  
iteration I am moving things to model as much as possible. My rule of  
thump is that you should not be using Model.find() in your controller.  
Write helper methods in your model to get the data in the form you  
want. That will lead to more consistency and leaner controller. But  
take care not to take this to insane levels 🙂

Also checkout this blog post for more details from some one with more  
clue than me.  
[http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model](http://weblog.jamisbuck.org/2006/10/18/skinny-controller-fat-model)

raj

---

<div class="post-metadata">

**Author:** ![pervel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7bcc69/32.png) [@pervel](https://www.ruby-forum.com/u/pervel)\
**Post date:** [January 9, 2007, 4:11pm UTC](https://www.ruby-forum.com/t/put-code-in-model-or-controller-any-general-rules-of-thumb/81464/4 "2007-01-09T16:11:21Z")

</div>

Sheldon H. wrote:

> You’re asking the right questions. 🙂
> 
> It took me a long time to settle on a simple decision process that  
> works for me, especially in the J2EE environment where it’s not as easy  
> to change your mind.

Sheldon,

Your entire response was great! I really enjoyed reading it and I don’t  
believe anyone could have said it any better… Thankfully I have no  
scruples, so I will simply steal yours and next time I get this  
question, I will be sure to impress! 🙂

ilan

---

<div class="post-metadata">

**Author:** ![pervel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7bcc69/32.png) [@pervel](https://www.ruby-forum.com/u/pervel)\
**Post date:** [January 9, 2007, 8:04am UTC](https://www.ruby-forum.com/t/put-code-in-model-or-controller-any-general-rules-of-thumb/81464/5 "2007-01-09T08:04:38Z")

</div>

You’re asking the right questions. 🙂

It took me a long time to settle on a simple decision process that  
works for me, especially in the J2EE environment where it’s not as easy  
to change your mind.

I default to trying to push code down into the model. However, I ask  
myself these two questions:

1. Does this code describe anything other than behavior of this model?
2. Does this code know about CGI or HTML?

If the answer to either of those questions is yes, I look more  
seriously at putting the code in the controller or an helper. If the  
answer is “yes and no”, I’m probably trying to do too much at once, and  
need to break it up across the layers.

In summary, favor the model, but not at the cost of layering  
violations.

The attitude that this comes from is that your domain model (your  
collection of “models”) is the system. The controller is only there to  
mediate a view into it. This attitude works well for the kinds of  
applications Rails is suited to.

Here are two more considerations:

1. Code that you put in the model is easy to use from the Rails console  
and runner. Code that’s in the controller is more of a pain in the arse  
to get to.

2. Model instances can call each others’ methods, but they can’t call  
methods on controllers without jumping through hoops.

Ciao,  
Sheldon.
