Age calculation, where to put it?

Hi there:

I was trying to figure out where to put a simple block of code to
calculate the age of every "user" I have in the table "people".
In the controller I get all the users from the model like this:
  @people = Person.find(:all)
Then in the view, when I'm showing all the attributes of each user I
do this:
  <% for p in people %>
    <tr>
    ...
    "age calculation and showing"
    ...
    </tr>
  <% end %>

Isn't this breaking the MVC theory?. Shouldn't put I this piece of
code at the controller?.

Cheers, Ibon.

The calculation should be in the Person class, something like

class Person
   def age(on=Date.today)
     ...
   end
end

Then you can return the age as a number (Integer?). The format could either be inline in your view or in a helper (formatted_age(p.age)) if it appears in multiple views.

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

Oh, thank you, I understand better MVC now :slight_smile:

Cheers, Ibon.