[Rails] ActiveRecord selecting multiple fields
Marten Veldthuis
marten at standardbehaviour.com
Sun Nov 7 15:14:34 GMT 2004
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 at mac.com> 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
More information about the Rails
mailing list