Posts Tagged ‘Ruby’

What is wrong with the MVCs I see?

January 11, 2006

Some 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
model(:recipe)

def index()
@recipes = Recipe.find_all()
end
end

With an accompanying model:
class Recipe < ActiveRecord::Base
belongs_to(:category)
end

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:


Richard.find_all()
paraphrased
"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.

I’m just searching for the truth.
the truth is out there
And the truth is out there.