# MVC design question

**URL:** <https://www.ruby-forum.com/t/mvc-design-question/103328>\
**Category:** Rails\
**Created:** [May 4, 2007, 11:00am UTC](https://www.ruby-forum.com/t/mvc-design-question/103328 "2007-05-04T11:00:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mlangenberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a87d85/32.png) [@mlangenberg](https://www.ruby-forum.com/u/mlangenberg)\
**Post date:** [May 4, 2007, 11:00am UTC](https://www.ruby-forum.com/t/mvc-design-question/103328/1 "2007-05-04T11:00:58Z")

</div>

How much business logic is actually allowed in a (Rails) controller?  
In the attachement I’ve put down three different approaches to the  
same problem.  
Which if the three approaches is preferable?

1. Calling Payment.create! directly.
2. Calling Payment.create! from Order#add\_payment wrapper.
3. Calling Payment.create! from Order#add\_payment wrapper without  
checking Order#payable from controller.

Am I violating any design patters with the last, and is it wise to  
wrap around the Payment.create instance method in Order#add\_payment?

---

<div class="post-metadata">

**Author:** ![mlangenberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a87d85/32.png) [@mlangenberg](https://www.ruby-forum.com/u/mlangenberg)\
**Post date:** [May 4, 2007, 3:50pm UTC](https://www.ruby-forum.com/t/mvc-design-question/103328/2 "2007-05-04T15:50:38Z")

</div>

Since this is a philosophical question you are bound to get many  
different answers, no one answer will be “it” or correct for all  
situations.

Item 1 is clear to the reader what is happening but requires the  
controller to know about how the models relate to each other.  
Item 2 has the advantage that how order and payment are related is not  
known by the controller, just that they are related.

I think that the add\_payment method should do any checking since its  
value is to hide the implementation of the connection between the two  
classes from the controller. If the controller knows what relation to  
test it serves no purpose to hide that in the add\_payment method.

Michael

On May 4, 2:00 am, “Matthijs L.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![mlangenberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a87d85/32.png) [@mlangenberg](https://www.ruby-forum.com/u/mlangenberg)\
**Post date:** [May 4, 2007, 3:53pm UTC](https://www.ruby-forum.com/t/mvc-design-question/103328/3 "2007-05-04T15:53:38Z")

</div>

Matthijs L. wrote:

> How much business logic is actually allowed in a (Rails) controller?  
> In the attachement I’ve put down three different approaches to the  
> same problem.  
> Which if the three approaches is preferable?
> 
> 1. Calling Payment.create! directly.
> 2. Calling Payment.create! from Order#add\_payment wrapper.
> 3. Calling Payment.create! from Order#add\_payment wrapper without  
> checking Order#payable from controller.
> 
> Am I violating any design patters with the last, and is it wise to  
> wrap around the Payment.create instance method in Order#add\_payment?

Matthijs,

While it’s advised to keep away from putting business logic in your  
controllers, it’s meant more as a guideline than a hard fast rule.  
Secondly, you can not “violate” a design pattern as they themselves are  
well established guidelines that sometimes have to be modified to meet  
your specific problem domain.

Your methods out of context provide little to no clue as to whether they  
belong in the controller or not… Don’t worry too much about it and  
while you are writing your app, just think to yourself, if the code that  
you are writing has to to with manipulating your model it’s probably  
belongs in the model (bussiness layer) and if it pertains to organizing  
models to feed to your views, it probably belongs in the controller…

The difference between question 2 and 3 has nothing to do with design  
patterns. It pertains specifically to the requirements of your problem  
domain.

hope this helps  
ilan

---

<div class="post-metadata">

**Author:** ![mlangenberg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a87d85/32.png) [@mlangenberg](https://www.ruby-forum.com/u/mlangenberg)\
**Post date:** [May 4, 2007, 8:29pm UTC](https://www.ruby-forum.com/t/mvc-design-question/103328/4 "2007-05-04T20:29:35Z")

</div>

Thanks for these very helpful replies, I’ve chosen to call  
Order#add\_payment  
directly from the PaymentController without any conditions, besides  
checking  
the parameters, which is a controller thing.  
Order#add\_payment calls Payment.create! on certain conditions. And  
raises an  
exception if those conditions aren’t met e.g. Order#payable? returns  
false.  
Since there are several conditions on which Payment.create! should not  
be  
called, Order#add\_payment raises specific exceptions for each of those  
conditions, this way I can provide error messages to the users without  
having a bunch of conditions in PaymentController#create which /must/ be  
called in order to keep the business logic alive.
