minutes hours ago, in a phone conversation with a dear friend, who also happens to be a decent programmer, I expressed my feeling about the Model View Controller pattern Rails is claiming to adhere to. Something feels wrong. And not only MVC in Rails.
Say I have a bunch of recipes, all nicely assigned to a smaller subset of categories. (Does that sound familiar?)
In Rails, if I would want to show all recipes on the recipe index page.
My controller class would look like this:
class RecipeController < ApplicationController
@recipes = Recipe.find_all()
With an accompanying model:
class Recipe < ActiveRecord::Base
What disturbs me is the find_all() method in Recipe.
Yes, I understand that Recipe subclasses ActiveRecord. But I would not expect the Recipe class to know about all its kin:
"Say Richard, pass me a reference to all people on earth, will you?"
Would it make sense to have some other class that would know about Products and supply the controller with data?
Another thing that bothers me is the php’ish way of having Ruby code in templates.
<% @recipes.each do |recipe| %>
<%= recipe.name %>
<% end %>
Aren’t that model instances we’re massaging in our view? Doesn’t that make our view partly a controller too?
Looking for answers I found an in-depth article (MVC and web apps: oil and water) by Harry Fuecks on MVC and its applicability in web applications. Take your time to read the articles he links to too.
Please let me know what you think.
Pay attention. I’m not bashing Rails. Beautiful apps have been written in that framework. I’m a paying Basecamp user, for example.
And the truth is out there.