bramu.ss-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2009-Mar-18 04:56 UTC
active record logical cols
HI, I am new to Rails started project couple of weeks back. Here is my requirement please let me know the solution. When I do find on my active record class it returns all the columns in my database table. I want to add one or two more cols to the return record. Like Student.find returns dob and name I want to add age to it some thing like that. Columns that are not exist in the database table but I want to add to the return record. How can I do this?? Please reply asap I am totally struck on this. Thanks, Ramu. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
bramu.ss-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Like Student.find returns dob and name I want to add age to it some > thing like that. Columns that are not exist in the database table but > I want to add to the return record. How can I do this??Age would just be a method on the Student model calculated from DOB: class Student < ActiveRecord::Base def age age = x # I''ll assume you can figure out how to calculate age from DOB end Getting access to the existing attributes like date_of_birth is a simple as: self.date_of_birth # self in this case means the current instances of the Student class since this is an instance method. Remember that Ruby will return the value of the last line of the method automatically so you don''t have to include the return. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---