# Where put buissness logic?

**URL:** <https://www.ruby-forum.com/t/where-put-buissness-logic/103861>\
**Category:** Rails\
**Created:** [May 11, 2007, 2:00am UTC](https://www.ruby-forum.com/t/where-put-buissness-logic/103861 "2007-05-11T02:00:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![madiba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@madiba](https://www.ruby-forum.com/u/madiba)\
**Post date:** [May 11, 2007, 2:00am UTC](https://www.ruby-forum.com/t/where-put-buissness-logic/103861/1 "2007-05-11T02:00:50Z")

</div>

I’ve always think that it belongs to controller but I’ve read few  
articles that it should be done by model and controller should be thin  
glue betwean model and view. Where should I put it?

## Regards

I’ve probably left my head… somewhere. Please wait untill I find it.  
Homepage (pl\_PL): [http://uzytkownik.jogger.pl/](http://uzytkownik.jogger.pl/)  
(GNU/)Linux User: #425935 (see [http://counter.li.org/](http://counter.li.org/))

---

<div class="post-metadata">

**Author:** ![madiba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@madiba](https://www.ruby-forum.com/u/madiba)\
**Post date:** [May 11, 2007, 2:53am UTC](https://www.ruby-forum.com/t/where-put-buissness-logic/103861/2 "2007-05-11T02:53:15Z")

</div>

Maciej P. wrote:

> I’ve always think that it belongs to controller but I’ve read few  
> articles that it should be done by model and controller should be thin  
> glue betwean model and view. Where should I put it?

Thick models, thin controllers and dumb views. That’s the general idea  
with Rails (or any MVC architecture).

There’s been plenty of discussion on this board and other web sites in  
the past. Look around; I’m sure you’ll find some useful stuff.

A good rule of thumb is this: your application should be completely  
usable from console, without any controllers or views.

Hope that helps!

---

<div class="post-metadata">

**Author:** ![madiba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@madiba](https://www.ruby-forum.com/u/madiba)\
**Post date:** [May 11, 2007, 9:50am UTC](https://www.ruby-forum.com/t/where-put-buissness-logic/103861/3 "2007-05-11T09:50:35Z")

</div>

Well, we’ve been having a big discussion on this subject on Perl’s  
Catalyst mailinglist and it seems to be common consesus that your  
business logic is outside of the web application and that the model is a  
thin wrpaper around that.

It depends on what your doing, as well.

So, there are lots of factors but, try to keep you business logic out of  
the controller as it should be pretty dumb (in my opinion)

-Ants

Maciej P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
I’ve always think that it belongs to controller but I’ve read few  
articles that it should be done by model and controller should be thin  
glue betwean model and view. Where should I put it?

## Regards

I’ve probably left my head… somewhere. Please wait untill I find it.  
Homepage (pl\_PL): [http://uzytkownik.jogger.pl/](http://uzytkownik.jogger.pl/)  
(GNU/)Linux User: #425935 (see [http://counter.li.org/](http://counter.li.org/))

* * *

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

---

<div class="post-metadata">

**Author:** ![madiba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@madiba](https://www.ruby-forum.com/u/madiba)\
**Post date:** [May 12, 2007, 12:44am UTC](https://www.ruby-forum.com/t/where-put-buissness-logic/103861/4 "2007-05-12T00:44:38Z")

</div>

The conflicting information is due to the many ways that MVC is being  
misused. MVC is a pattern, patterns are guidelines and are often  
interpreted in many different ways. MVC is not a complete pattern.  
There are many other patterns that can be appended to the MVC, such as  
delegate patterns, observer patterns, etc.

Here is my opinion on the subject: Every piece of logic has it’s  
place. The job of the developer is to “discover” where that place  
is. If it’s not initially clear where it belongs, then it’s likely  
that you don’t yet completely understand the problem domain.

So you ask yourself these simple questions:

1. Does this logic manipulate the data in anyway?
2. Does this logic support translating (but not modifying) the data  
and communicate that data to some form of presentation?
3. Is this logic purely manipulating how the data is presented?
4. Is this support logic that doesn’t fit anywhere in the above  
questions, or is it useful to all three.

If 1 you have model logic.  
if 2 you have controller logic.  
if 3 you have view logic.  
if 4 then you have either helper, or library logic.

If you can answer yes to more that one of these questions then you  
have discovered where the logic need to be separated.

---

<div class="post-metadata">

**Author:** ![madiba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@madiba](https://www.ruby-forum.com/u/madiba)\
**Post date:** [May 12, 2007, 12:11am UTC](https://www.ruby-forum.com/t/where-put-buissness-logic/103861/5 "2007-05-12T00:11:35Z")

</div>

On Fri, 11 May 2007 02:53:15 +0200, Daniel W. wrote:

> Maciej P. wrote:
> 
> > I’ve always think that it belongs to controller but I’ve read few  
> > articles that it should be done by model and controller should be thin  
> > glue betwean model and view. Where should I put it?
> 
> Thick models, thin controllers and dumb views. That’s the general idea  
> with Rails (or any MVC architecture).

Thanks. I heard many contradictive things about MVC.

## Regards

I’ve probably left my head… somewhere. Please wait untill I find it.  
Homepage (pl\_PL): [http://uzytkownik.jogger.pl/](http://uzytkownik.jogger.pl/)  
(GNU/)Linux User: #425935 (see [http://counter.li.org/](http://counter.li.org/))

---

<div class="post-metadata">

**Author:** ![madiba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@madiba](https://www.ruby-forum.com/u/madiba)\
**Post date:** [May 22, 2007, 1:19pm UTC](https://www.ruby-forum.com/t/where-put-buissness-logic/103861/6 "2007-05-22T13:19:58Z")

</div>

I’ve been just pondering this item and have seen conflicting info in the  
“Agile Development with Rails” book, probably due to the manner in which  
“business logic” is overloaded.

Robert - I liked your summation.

What do people think about the case: I have a high level “save” that  
incorporates saving the parent and child data. The per field  
validations  
are already covered by the rails framework, however what about business  
logic cross checks I need to do with the data, e.g. there can only be  
one  
row with a given “event\_type” for each “trans\_id”, or another way:  
records  
which have transaction id X can have only one entry for which  
“event\_type” =  
Y. So:

(a) Could I assume this is a higher level business logic validation  
check,  
and incorporate this in the model (makes most sense to me short of  
creating  
another layer)

(b) Given that it is in the model what approach would be best to pass  
this  
back to the controller layer: (a) exceptions, (b) use of rails  
validation  
framework, or © custom business logic validation framework? It won’t  
be  
a validation error that can align with a view field, but then again it’s  
not  
an exception in the sense of a system error either. Kind of a business  
logic validation alert.

Thanks  
Greg

---

<div class="post-metadata">

**Author:** ![madiba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58956e/32.png) [@madiba](https://www.ruby-forum.com/u/madiba)\
**Post date:** [May 22, 2007, 6:59pm UTC](https://www.ruby-forum.com/t/where-put-buissness-logic/103861/7 "2007-05-22T18:59:03Z")

</div>

Your case definitely sounds like model logic.

To me I always think something belongs in the model if you want the  
rule to always apply, regardless of the controller or action in a  
controller.

So in your case where there can only be one row with a given  
“event\_type”, then this needs to be enforced every time the data is  
manipulated, it is not controller specific.

Regarding how you might pass this back to the controller… sounds  
like some custom validation could do the trick, it depends when you  
need the user to be notified really.

Hope that helps clarify things,  
Paul.

> **[paulsturgess.co.uk](https://paulsturgess.co.uk)**
>
> Paul Sturgess – Full stack software engineer and tech lead.
