# Age calculation, where to put it?

**URL:** <https://www.ruby-forum.com/t/age-calculation-where-to-put-it/146723>\
**Category:** Rails\
**Created:** [August 24, 2008, 7:30pm UTC](https://www.ruby-forum.com/t/age-calculation-where-to-put-it/146723 "2008-08-24T19:30:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![txapelgorri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5f8ce5/32.png) [@txapelgorri](https://www.ruby-forum.com/u/txapelgorri)\
**Post date:** [August 24, 2008, 7:30pm UTC](https://www.ruby-forum.com/t/age-calculation-where-to-put-it/146723/1 "2008-08-24T19:30:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![txapelgorri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5f8ce5/32.png) [@txapelgorri](https://www.ruby-forum.com/u/txapelgorri)\
**Post date:** [August 24, 2008, 7:47pm UTC](https://www.ruby-forum.com/t/age-calculation-where-to-put-it/146723/2 "2008-08-24T19:47:38Z")

</div>

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](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![txapelgorri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5f8ce5/32.png) [@txapelgorri](https://www.ruby-forum.com/u/txapelgorri)\
**Post date:** [August 24, 2008, 8:29pm UTC](https://www.ruby-forum.com/t/age-calculation-where-to-put-it/146723/3 "2008-08-24T20:29:46Z")

</div>

Oh, thank you, I understand better MVC now 🙂

Cheers, Ibon.
