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 %>



“age calculation and showing”


<% end %>

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

Cheers, Ibon.

On Aug 24, 2008, at 1:28 PM, txapelgorri wrote:


“age calculation and showing”

<% 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 B. http://agileconsultingllc.com
[email protected]

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

Cheers, Ibon.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs