I'm an ASP.NET MVC developer just starting with my first big project on rails however Im confused as where to put your business logic? on ASP.NET I create a library which contains services(Domain driven design) which handle business logic, I have heard that rails uses a concept of fat model skinny controller but I have some projects in ASP.NET which adding all the logic to the controller would create a big mess, is there any other way?
-
1what do you mean by "business logic"? – sethvargo Dec 29 '10 at 23:38
-
you can also try creating modules and putting them in your lib directory – stephenmurdoch Dec 29 '10 at 23:38
4 Answers
Go with the concept of FatModels and SkinnyControllers. Your models should know how they behave and what they should do.
When your models get too fat, extract them out into re-usuable modules and include them in your module.
- Example of taking a fat controller (with logic) and moving to a model
- Example of taking code from the views and moving into the model
You can easily test behavior of models using RSpec (or test/unit or shoulda). Then you can test that the application behaves correctly using Cucumber.

- 40,197
- 4
- 83
- 109
-
6Hi Future readers! The above makes sense, but rather that keep fattening up your model, check out http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ – Jesse Wolgamott Feb 08 '13 at 16:07
"Business Logic" or some might call it "Domain Logic" doesn't belong anywhere near Rails and/or your .NET MVC project. Rails and MVC should depend on your Domain not the other way around. I would recommend reading up on the Onion Architecture from Jeffery Palermo or watch "Architecture the Lost Years" by Robert Martin. (I think that's that talk anyway). There are probably more resources than that, but you'll thank yourself later for treating both Rails and .NET MVC like the 3rd party frameworks they are, and not the main house of your application.

- 14,660
- 17
- 61
- 66
-
8i've seen the talk and have heard this idea expressed many times, but I've never seen a concrete example or demonstration of how to achieve this. as a novice, it makes sense in principle, but I can't really conceptualize how to bring this to practice. do you have an example of a rails app that follows this practice that I could look at, or an article with a more concrete example? – o_o_o-- Jun 02 '13 at 06:08
-
1this is true but kinda also not true for ruby. Ruby (especially with active records) is intended to use MVC as the whole application architecture and not just as a presentation layer pattern. Thus M (Business logic and DAL) C (Application logic) and V (Presentation Logic). This of course has draw backs for big complex appliaction (eg. where DDD is needed). But ruby was intended for simple and fast implemented applications and it comes with a lot out of the box features built on&for Active Records – S. John Fagone Sep 05 '21 at 22:26
I think this blog article provides a good overview of a strategy of incorporating domain driven design with in the rails framework: http://www.smashingboxes.com/domain-logic-in-rails/
TL;DR
Refactor your classic rails models into repositories, and use a facade layer in the controllers to interact with your domain model.
I'm struggling with this a little my self, and as much as the Fat Controller pattern seems to prevail, anything "fat" in software seems to be a smell, violating single responsibility.
You can put business logic anywhere you want (even in views! though that's a bad idea).
I'd say if the logic is tied to a real-world object, then put it on the model. Otherwise, use the controller. But it's up to you to determine how to do it for your app. Models are for modeling things, and Controllers are for controlling things.

- 53,238
- 27
- 117
- 198