Forum: Ruby on Rails find query

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.
Ed78f6c66a6f29bd89c67eeaf4f69931?d=identicon&s=25 Petr Bobek (praethorian)
on 2008-12-01 09:48
Hi,

I have user table (last_activity, last_activity_at, ....) and
friendships table (many to many).
What I am trying to do is to show to the user the last actions of
his/her friends.

[code]@activities = User.find_by_username(username, :order =>
'last_activity_at DESC', :include => [:friendships => :friend])[/code]

[code]<%= render :partial => 'friends_activity', :collection =>
@activities.friendships %>[/code]

But the descending ordering is not working :( What I am missing here?
Cheers, Pete
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-01 10:25
(Received via mailing list)
On 1 Dec 2008, at 08:48, Petr Bobek wrote:

>
> [code]<%= render :partial => 'friends_activity', :collection =>
> @activities.friendships %>[/code]
>
> But the descending ordering is not working :( What I am missing here?
> Cheers, Pete

That sort would have ordered the users (from the 'main' find) by
descending last_activity_at, not the associations (which are loaded
with a separate find)

Fred
Ed78f6c66a6f29bd89c67eeaf4f69931?d=identicon&s=25 Petr Bobek (praethorian)
on 2008-12-01 10:47
> That sort would have ordered the users (from the 'main' find) by
> descending last_activity_at, not the associations (which are loaded
> with a separate find)
>
> Fred

Ohh I see, I was trying to sort friendships table, but there is no
column last_activity_at. I guess that's it. :( or is there a way to
re-sort the data by last_activity_at ? Cheers
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-01 12:28
(Received via mailing list)
On Dec 1, 9:47 am, Petr Bobek <rails-mailing-l...@andreas-s.net>
wrote:
> > That sort would have ordered the users (from the 'main' find) by
> > descending last_activity_at, not the associations (which are loaded
> > with a separate find)
>
> > Fred
>
> Ohh I see, I was trying to sort friendships table, but there is no
> column last_activity_at. I guess that's it. :( or is there a way to
> re-sort the data by last_activity_at ? Cheers

The old style include that did one big join would probably do that for
you.
Just to be sure  - I'm assuming that friendships and friends are a
self referential has many though relationship (ie back to users ?)

I'd write the above query like this:
u = User.find_by_username ...
u.friendships.find :all, :joins => :friends, :order =>
'last_activity_at desc'

You could squish that down to one query, but you'll have to work out
how it aliases the table names (I always have to look that one up)

Fred
Ed78f6c66a6f29bd89c67eeaf4f69931?d=identicon&s=25 Petr Bobek (praethorian)
on 2008-12-01 15:12
> I'd write the above query like this:
> u = User.find_by_username ...
> u.friendships.find :all, :joins => :friends, :order =>
> 'last_activity_at desc'

Cheers Fred, this is the trick I was looking for.
Petr
This topic is locked and can not be replied to.