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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Aug 24, 2008, at 1:28 PM, txapelgorri wrote:> 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-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Oh, thank you, I understand better MVC now :) Cheers, Ibon. On 24 ago, 19:46, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> On Aug 24, 2008, at 1:28 PM, txapelgorri wrote: > > > > > 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 > R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---