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.
on 2009-03-18 06:56
on 2009-03-18 08:07
firstname.lastname@example.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.