# Find query

**URL:** <https://www.ruby-forum.com/t/find-query/154527>\
**Category:** Rails\
**Created:** [December 1, 2008, 9:48am UTC](https://www.ruby-forum.com/t/find-query/154527 "2008-12-01T09:48:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![golf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@golf](https://www.ruby-forum.com/u/golf)\
**Post date:** [December 1, 2008, 9:48am UTC](https://www.ruby-forum.com/t/find-query/154527/1 "2008-12-01T09:48:42Z")

</div>

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.

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

`<%= render :partial => 'friends_activity', :collection =>
@activities.friendships %>`

But the descending ordering is not working ☹ What I am missing here?  
Cheers, Pete

---

<div class="post-metadata">

**Author:** ![golf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@golf](https://www.ruby-forum.com/u/golf)\
**Post date:** [December 1, 2008, 10:25am UTC](https://www.ruby-forum.com/t/find-query/154527/2 "2008-12-01T10:25:44Z")

</div>

On 1 Dec 2008, at 08:48, Petr B. wrote:

> `<%= render :partial => 'friends_activity', :collection =>
> @activities.friendships %>`
> 
> 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

---

<div class="post-metadata">

**Author:** ![golf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@golf](https://www.ruby-forum.com/u/golf)\
**Post date:** [December 1, 2008, 10:47am UTC](https://www.ruby-forum.com/t/find-query/154527/3 "2008-12-01T10:47:11Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![golf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@golf](https://www.ruby-forum.com/u/golf)\
**Post date:** [December 1, 2008, 3:12pm UTC](https://www.ruby-forum.com/t/find-query/154527/4 "2008-12-01T15:12:04Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![golf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/90db22/32.png) [@golf](https://www.ruby-forum.com/u/golf)\
**Post date:** [December 1, 2008, 12:28pm UTC](https://www.ruby-forum.com/t/find-query/154527/5 "2008-12-01T12:28:30Z")

</div>

On Dec 1, 9:47 am, Petr B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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
