# Sorting with relationships? \[total noob\]

**URL:** <https://www.ruby-forum.com/t/sorting-with-relationships-total-noob/147058>\
**Category:** Rails\
**Created:** [August 28, 2008, 3:57am UTC](https://www.ruby-forum.com/t/sorting-with-relationships-total-noob/147058 "2008-08-28T03:57:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![noob2everything](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e8c25b/32.png) [@noob2everything](https://www.ruby-forum.com/u/noob2everything)\
**Post date:** [August 28, 2008, 3:57am UTC](https://www.ruby-forum.com/t/sorting-with-relationships-total-noob/147058/1 "2008-08-28T03:57:07Z")

</div>

Hello. I have what i assume is a simple problem. I have want to be able  
to sort a dataset by referencing the text name of a related class.

For example: let’s pretend I have these models: “things” and “colors”.

Things “belongs\_to” Colors and Colors “has\_one” things.

Thing.color\_id = Color.id

So, Thing.color.name = Color.name where Thing.color\_id == Color.id

You get it.

So, I want to do this:

mythings = Thing.find(:all, :order =\> _on Thing.color.name_)

I want to be able to sort all my things by the colors (red, blue, etc)  
by name and not their ID in the database.

---

<div class="post-metadata">

**Author:** ![noob2everything](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e8c25b/32.png) [@noob2everything](https://www.ruby-forum.com/u/noob2everything)\
**Post date:** [August 28, 2008, 10:34am UTC](https://www.ruby-forum.com/t/sorting-with-relationships-total-noob/147058/2 "2008-08-28T10:34:46Z")

</div>

On 28 Aug 2008, at 03:27, Joshua A. wrote:

> Try this:
> 
> mythings = Thing.find(:all, :include =\> :color, :order =\> ‘colors.name  
> asc’)

That will work, but really all you need is :joins. You just need to  
join the colours table, not do all the instantiating of the extra  
objects.

Fred

---

<div class="post-metadata">

**Author:** ![noob2everything](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e8c25b/32.png) [@noob2everything](https://www.ruby-forum.com/u/noob2everything)\
**Post date:** [August 28, 2008, 4:27am UTC](https://www.ruby-forum.com/t/sorting-with-relationships-total-noob/147058/3 "2008-08-28T04:27:02Z")

</div>

Try this:

mythings = Thing.find(:all, :include =\> :color, :order =\> ‘colors.name  
asc’)

– Josh

Nick Norton wrote:

> Hello. I have what i assume is a simple problem. I have want to be able  
> to sort a dataset by referencing the text name of a related class.
> 
> For example: let’s pretend I have these models: “things” and “colors”.
> 
> Things “belongs\_to” Colors and Colors “has\_one” things.
> 
> Thing.color\_id = Color.id
> 
> So, Thing.color.name = Color.name where Thing.color\_id == Color.id
> 
> You get it.
> 
> So, I want to do this:
> 
> mythings = Thing.find(:all, :order =\> _on Thing.color.name_)
> 
> I want to be able to sort all my things by the colors (red, blue, etc)  
> by name and not their ID in the database.
