Forum: Ruby on Rails Put code in model or controller. Any general rules of thumb?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1caf2caa2872edcb15be0e52fd442bab?d=identicon&s=25 Per Velschow (Guest)
on 2007-01-09 03:59
(Received via mailing list)
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. :)
3f7fc5fbdbb40cf38d5bf94f265b343c?d=identicon&s=25 Andrew Skegg (askegg)
on 2007-01-09 04:25
Per Velschow 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-cont...
25cc4b4f00c2adcef7dd775576adad1e?d=identicon&s=25 Rajkumar S (Guest)
on 2007-01-09 06:52
(Received via mailing list)
On 1/3/07, Per Velschow <Per.Velschow@gmail.com> 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-cont...

raj
A2153abbc2b5b7085014bf0a1f9fd28a?d=identicon&s=25 Sheldon Hearn (Guest)
on 2007-01-09 08:04
(Received via mailing list)
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.
391f9b787cdc12aa2c179713f5103e3a?d=identicon&s=25 Ilan Berci (iberci)
on 2007-01-09 16:11
Sheldon Hearn 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
This topic is locked and can not be replied to.