There’s an episode of The Simpsons where Homer’s inner-child point’s to his mouth saying “Food goes in here.” To which Homer replies “It sure does.”

Model<= Business logic goes in here

It sure does.

As a general rule I avoid blogging about the obvious, but sometimes what’s obvious varies from person to person. I’ve run into the misconception that Rails does not support this obvious statement, that business logic goes into the Model. In Rails, according to this misconception, the Model is simply a Data Access Layer, and business logic winds up in the helpers or elsewhere. The purpose of this post is mainly to give me something to link to or copy and paste as the mood strikes me when I next run into this misconception.

In Ruby on Rails, as in any good MVC framework, business logic goes in the Model. All of it. Every shred of it.

I’m currently working on a project to develop a point-of-sale and online purchasing system with a great deal of very complex (some might argue overly complex) business logic. As a simpler example, displaying the available quantity of a specific item involves considering at which warehouse the order is being fulfilled, the quantities in various bins, and quantities from the bins which have already been allocated to an order but not yet pulled.

None of that business logic is in the Controller. None of it is in the View. And none of it is in a helper. It’s all in the Model. The only thing the View needs to do is this:
I’m not sure where this notion that business logic goes in the helper comes from. For the record:

A helper is simply a module containing methods that assist a view. Helper methods are output-centric. They exist to generate HTML (or XML, or JavaScript)—a helper extends the behavior of a template.

Agile Web Development With Rails, Dave Thomas, David Heinemeier Hansson

Put another way, you can off-load code to the helper to avoid putting too much into your View templates.

For further reading on this topic, here’s a few handy links: