Hello, I have a model for a Person and I am trying to get the "first_name" and the "last_name" which I have in different columns in a table. Is this the most efficient way? def self.getFullName(user_id) find(user_id).first_names + " " + find(user_id).last_name end I assume that this makes two queries ... I would like to only make one (also for future stuff ... how do I make this more efficient?) Thank you in advance, /B -- Bruno Mattarollo <bmatt-ee4meeAH724@public.gmane.org> Currently in: Sydney, Australia [ http://pokies.typepad.com/virtual_possum/ ] _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
def fullname "#{first_name} #{self.last_name}" end person = Person.find(1) person.fullname On Mon, 8 Nov 2004 00:32:20 +1100, Bruno Mattarollo <bmatt-ee4meeAH724@public.gmane.org> wrote:> Hello, > > I have a model for a Person and I am trying to get the "first_name" and > the "last_name" which I have in different columns in a table. Is this > the most efficient way? > > def self.getFullName(user_id) > find(user_id).first_names + " " + find(user_id).last_name > end > > I assume that this makes two queries ... I would like to only make one > (also for future stuff ... how do I make this more efficient?) > > Thank you in advance, > > /B > > -- > Bruno Mattarollo <bmatt-ee4meeAH724@public.gmane.org> > Currently in: Sydney, Australia > [ http://pokies.typepad.com/virtual_possum/ ] > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >-- Tobi
Tobias Luetke wrote:> def fullname > "#{first_name} #{self.last_name}" > end > > person = Person.find(1) > person.fullname > > > On Mon, 8 Nov 2004 00:32:20 +1100, Bruno Mattarollo <bmatt-ee4meeAH724@public.gmane.org> wrote: > >>Hello, >> >>I have a model for a Person and I am trying to get the "first_name" and >>the "last_name" which I have in different columns in a table. Is this >>the most efficient way? >> >>def self.getFullName(user_id) >> find(user_id).first_names + " " + find(user_id).last_name >> end >> >>I assume that this makes two queries ... I would like to only make one >>(also for future stuff ... how do I make this more efficient?) >>Or you could use an aggregation for this (might be overkill in this case): http://ar.rubyonrails.org/classes/ActiveRecord/Aggregations/ClassMethods.html -- Marten Veldthuis