Forum: Ruby on Rails sorting issue

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Chris S. (Guest)
on 2006-01-08 23:02
I've got the following setup:

category (hm) <-> (bt) activity

An activity has an amount, date, location, note, and category. I'm
sorting my activities table by category_id (foreign key in activity),
but how do I sort it by category_name instead? Basically I just need
category_name in the result set. Here's what I have now:


@all = Activity.find(:all, :order => 'category')

How do I grab the category_name column from Category so that it is part
of @all?

Thanks!
Marcel Molina Jr. (Guest)
on 2006-01-08 23:05
(Received via mailing list)
On Sun, Jan 08, 2006 at 10:02:36PM +0100, Chris S. wrote:
>
> How do I grab the category_name column from Category so that it is part
> of @all?

You can eagerly load the category then reference one of its columns in
the
:order.

Activity.find(:all, :include => :category, :order => 'categories.name')

marcel
Chris S. (Guest)
on 2006-01-08 23:13
Marcel Molina Jr. wrote:
> On Sun, Jan 08, 2006 at 10:02:36PM +0100, Chris S. wrote:
>>
>> How do I grab the category_name column from Category so that it is part
>> of @all?
>
> You can eagerly load the category then reference one of its columns in
> the
> :order.
>
> Activity.find(:all, :include => :category, :order => 'categories.name')
>
> marcel

Awesome, thanks. I knew it was something straightforward.
This topic is locked and can not be replied to.