Rails: Skinny Controller vs. Fat Model, or should I make my Controller Anorexic?

There is an old saying Smart data structures and dumb code works a lot better than the other way around Everything you have described is about how one piece of data relates to another. That itself is your clue that the logic for those relationships, including validation should be in the model or database layer An anorexic controller is a sign of well-designed (smart? ) data.My experience tells me that the more effort you put into designing your data, the less code you have to write overall Controllers are best at parsing inputs, calling the appropriate models, and then formatting the outputs.

There is an old saying, Smart data structures and dumb code works a lot better than the other way around. Everything you have described is about how one piece of data relates to another. That itself is your clue that the logic for those relationships, including validation should be in the model or database layer.An anorexic controller is a sign of well-designed (smart?

) data. My experience tells me that the more effort you put into designing your data, the less code you have to write overall. Controllers are best at parsing inputs, calling the appropriate models, and then formatting the outputs.

Very interesting. I'm glad to have read this... and also to have looked up the origin of that saying: The Cathedral and the Bazaar – daze Aug 2 at 5:38.

I would put the logic in my model, especially if I'm TDD'ing (and I always TDD, except when I don't. ) Testing the model is usually much easier than testing the controller.

It also means that you don't need to validate in each of your controllers. – SeanJA Mar 31 '10 at 4:05.

I like to approach questions like this by thinking about responsibility. What in this case is "responsible" for verifying the VIN? The model.

The controller is simply there to pass along the parameters...to "control" based on user input. If it's not entirely clear, think of it this way: where will putting this code cause the least amount of impact if it needs to be re-used? Say...if two different actions in two different controllers both need to verify a VIN, what would need to be done?

If you left this code in the controller, you'd essentially have to duplicate it in the new controller as well, but if you had placed it in the model, you'd simply call the check_vin method from the new controller, and no code duplication would be needed. By assigning responsibilities where they make the most sense, you've improved the re-usability of your code.

That usually straightens out where code should go. – Jonathan Julian Mar 31 '10 at 10:53.

It's the controller's responsibility to "parse" params, but validation should be done in the model. I would do something like this on the controller: @my_model = MyModel. New(:source => params:source ...) if(@my_model.

Valid? ) # treat valid model here (i.e. Save the model and redirect to show) else # treat validation error here (usually throw an error) end Your controller will actually be just "skinny".

For truly "anorexic" controllers, you might want to have a look at inherited_resources or resource_this. In some cases these will give you a 3-lines controller, implementing the whole RESTful stack.

The controller is simply there to pass along the parameters...to "control" based on user input. If it's not entirely clear, think of it this way: where will putting this code cause the least amount of impact if it needs to be re-used? Say...if two different actions in two different controllers both need to verify a VIN, what would need to be done?

If you left this code in the controller, you'd essentially have to duplicate it in the new controller as well, but if you had placed it in the model, you'd simply call the check_vin method from the new controller, and no code duplication would be needed. By assigning responsibilities where they make the most sense, you've improved the re-usability of your code.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions