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.
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)
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
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.