# Sorting by an attribute on a associated table

**URL:** <https://www.ruby-forum.com/t/sorting-by-an-attribute-on-a-associated-table/96757>\
**Category:** Rails\
**Created:** [July 1, 2006, 4:17pm UTC](https://www.ruby-forum.com/t/sorting-by-an-attribute-on-a-associated-table/96757 "2006-07-01T16:17:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bdeverea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f6c823/32.png) [@bdeverea](https://www.ruby-forum.com/u/bdeverea)\
**Post date:** [July 1, 2006, 4:17pm UTC](https://www.ruby-forum.com/t/sorting-by-an-attribute-on-a-associated-table/96757/1 "2006-07-01T16:17:34Z")

</div>

So, I have a User has\_many :profiles. The most recently dated profile is  
considered the current one. I need to sort these users by name, but the  
name attribute is a member of the associated table.

Is there any easy way to do this other than with some ugly find\_by\_sql?

Maybe a more general question - is there a common solution to sorting in  
Ruby?

---

<div class="post-metadata">

**Author:** ![bdeverea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f6c823/32.png) [@bdeverea](https://www.ruby-forum.com/u/bdeverea)\
**Post date:** [July 1, 2006, 4:37pm UTC](https://www.ruby-forum.com/t/sorting-by-an-attribute-on-a-associated-table/96757/2 "2006-07-01T16:37:53Z")

</div>

Something like

@user.profiles.find(:all, :order =\> “name DESC”)

Bryan D. wrote:

> So, I have a User has\_many :profiles. The most recently dated profile is  
> considered the current one. I need to sort these users by name, but the  
> name attribute is a member of the associated table.
> 
> Is there any easy way to do this other than with some ugly find\_by\_sql?
> 
> Maybe a more general question - is there a common solution to sorting in  
> Ruby?

---

<div class="post-metadata">

**Author:** ![bdeverea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f6c823/32.png) [@bdeverea](https://www.ruby-forum.com/u/bdeverea)\
**Post date:** [July 1, 2006, 4:40pm UTC](https://www.ruby-forum.com/t/sorting-by-an-attribute-on-a-associated-table/96757/3 "2006-07-01T16:40:17Z")

</div>

What about using acts\_as\_list, :order =\> :date\_added (or whatever you  
call that column)? Then you could say

def profile  
profilies.first  
end

… I think 🙂

---

<div class="post-metadata">

**Author:** ![bdeverea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f6c823/32.png) [@bdeverea](https://www.ruby-forum.com/u/bdeverea)\
**Post date:** [July 1, 2006, 4:45pm UTC](https://www.ruby-forum.com/t/sorting-by-an-attribute-on-a-associated-table/96757/4 "2006-07-01T16:45:26Z")

</div>

Perhaps I was unclear. I have no trouble sorting the profiles  
themselves.

What I want to do is sort the USERS by an attribute on their most recent  
profile. Something along the lines of:

@users = User.find(:all, :order =\> |user| {user.profile.firstname})

Of course I totally made that syntax up.

---

<div class="post-metadata">

**Author:** ![bdeverea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f6c823/32.png) [@bdeverea](https://www.ruby-forum.com/u/bdeverea)\
**Post date:** [July 1, 2006, 4:52pm UTC](https://www.ruby-forum.com/t/sorting-by-an-attribute-on-a-associated-table/96757/5 "2006-07-01T16:52:59Z")

</div>

Bryan D. wrote:

> 

I don’t know how to do this in SQL but I can give you some ruby that  
will get you going.

@users = User.find(:all)  
@users.sort\_by {|user| user.profile.firstname}

This will be really slow compared to a pure SQL sort so I would still  
look for a way to do that.

Matthew M.  
[blog.mattmargolis.net](http://blog.mattmargolis.net)

---

<div class="post-metadata">

**Author:** ![bdeverea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f6c823/32.png) [@bdeverea](https://www.ruby-forum.com/u/bdeverea)\
**Post date:** [July 2, 2006, 7:52pm UTC](https://www.ruby-forum.com/t/sorting-by-an-attribute-on-a-associated-table/96757/6 "2006-07-02T19:52:46Z")

</div>

Matthew M. wrote:

> I don’t know how to do this in SQL but I can give you some ruby that  
> will get you going.
> 
> @users = User.find(:all)  
> @users.sort\_by {|user| user.profile.firstname}
> 
> This will be really slow compared to a pure SQL sort so I would still  
> look for a way to do that.

Something like this should do the trick in pure SQL.

@users = User.find(:all, :include =\> :profile, :order =\>  
“profiles.firstname ASC”)

Or just do a join if you don’t want to instantiate all the profiles.

hth

---

<div class="post-metadata">

**Author:** ![bdeverea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f6c823/32.png) [@bdeverea](https://www.ruby-forum.com/u/bdeverea)\
**Post date:** [July 1, 2006, 5:16pm UTC](https://www.ruby-forum.com/t/sorting-by-an-attribute-on-a-associated-table/96757/7 "2006-07-01T17:16:38Z")

</div>

Matthew M. wrote:

> Bryan D. wrote:
> 
> > 
> 
> I don’t know how to do this in SQL but I can give you some ruby that  
> will get you going.
> 
> @users = User.find(:all)  
> @users.sort\_by {|user| user.profile.firstname}
> 
> This will be really slow compared to a pure SQL sort so I would still  
> look for a way to do that.
> 
> Matthew M.  
> [blog.mattmargolis.net](http://blog.mattmargolis.net)

Awesome, that’s exactly what I was looking for. Thanks!
