# How to order by a field in the belongs\_to association

**URL:** <https://www.ruby-forum.com/t/how-to-order-by-a-field-in-the-belongs-to-association/156151>\
**Category:** Rails\
**Created:** [December 21, 2008, 7:35pm UTC](https://www.ruby-forum.com/t/how-to-order-by-a-field-in-the-belongs-to-association/156151 "2008-12-21T19:35:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [December 21, 2008, 7:35pm UTC](https://www.ruby-forum.com/t/how-to-order-by-a-field-in-the-belongs-to-association/156151/1 "2008-12-21T19:35:33Z")

</div>

Consider this:  
class Document  
has\_many :revisions  
end  
class Revision  
belongs\_to :document  
end

Suppose I wanted to query all records in my revisions table, but I  
wanted to  
order the results by document.number. What sort of conditions would I  
have  
to impose on my call to Revision.find\_all? Is there a way to specify  
this  
in Ruby/Rails? Or will I need to (shudder) write some SQL?

–wpd

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [December 21, 2008, 8:25pm UTC](https://www.ruby-forum.com/t/how-to-order-by-a-field-in-the-belongs-to-association/156151/2 "2008-12-21T20:25:23Z")

</div>

On Sun, Dec 21, 2008 at 7:34 PM, Patrick D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Consider this:  
> class Document  
> has\_many :revisions  
> end  
> class Revision  
> belongs\_to :document  
> end  
> Suppose I wanted to query all records in my revisions table, but I wanted to  
> order the results by document.number. What sort of conditions would I have

Specify the :order param in your find, à la :order =\> ‘documents.number  
ASC’

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [December 21, 2008, 8:41pm UTC](https://www.ruby-forum.com/t/how-to-order-by-a-field-in-the-belongs-to-association/156151/3 "2008-12-21T20:41:48Z")

</div>

On Sun, Dec 21, 2008 at 2:24 PM, Franz S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> to
> 
> > order the results by document.number. What sort of conditions would I  
> > have
> 
> Specify the :order param in your find, Ã la :order =\> ‘documents.number  
> ASC’
> 
> I can’t figure out how to make that work – the revisions table does not  
> have a column named “documents”. It does have a “document\_id” field,  
> which  
> is associated with a specific record in the documents table. I’m trying  
> to  
> figure out how I can query the “revisions” table, but sort the results  
> based  
> on data stored in the associated record in the “documents” table.

–wpd

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [December 21, 2008, 9:56pm UTC](https://www.ruby-forum.com/t/how-to-order-by-a-field-in-the-belongs-to-association/156151/4 "2008-12-21T21:56:56Z")

</div>

On Sun, Dec 21, 2008 at 8:40 PM, Patrick D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I can’t figure out how to make that work – the revisions table does not  
> have a column named “documents”. It does have a “document\_id” field, which  
> is associated with a specific record in the documents table. I’m trying to  
> figure out how I can query the “revisions” table, but sort the results based  
> on data stored in the associated record in the “documents” table.

You’ll need to do a join to the documents table then, à la

@revisions = Revision.find(:all, :include =\> :document, :order =\>  
‘documents.sort\_field’)

Read up on ActiveRecord find for more info.

Regards,  
Franz

---

<div class="post-metadata">

**Author:** ![oldbug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@oldbug](https://www.ruby-forum.com/u/oldbug)\
**Post date:** [December 21, 2008, 11:41pm UTC](https://www.ruby-forum.com/t/how-to-order-by-a-field-in-the-belongs-to-association/156151/5 "2008-12-21T23:41:18Z")

</div>

> You’ll need to do a join to the documents table then, Ã la
> 
> @revisions = Revision.find(:all, :include =\> :document, :order =\>  
> ‘documents.sort\_field’)
> 
> Beautiful. That’s just what I was looking for.

–wpd
