# Sort collection by nested model

**URL:** <https://www.ruby-forum.com/t/sort-collection-by-nested-model/186988>\
**Category:** Rails\
**Created:** [April 15, 2010, 10:34am UTC](https://www.ruby-forum.com/t/sort-collection-by-nested-model/186988 "2010-04-15T10:34:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/96bed5/32.png) [@alexd](https://www.ruby-forum.com/u/alexd)\
**Post date:** [April 15, 2010, 10:34am UTC](https://www.ruby-forum.com/t/sort-collection-by-nested-model/186988/1 "2010-04-15T10:34:55Z")

</div>

Hi everyone,

I have GroupDocument model with following fields:  
belongs\_to :group  
belongs\_to :document  
enum\_attr :status, %w(pending approved banned)

Document has fields like ‘title’ or ‘author’. On controller index I  
do:

```
@group_documents =

```

GroupDocument.find\_all\_by\_group\_id(params[:group\_id]).paginate :page  
=\> params[:page], :per\_page =\> 10, :order =\> ‘?’

Problem is with order. I want to return @group\_documents collection  
ordered by document.title. Something like  
:order =\> ‘group.title asc’

How is it possible?

Cheers Alex

---

<div class="post-metadata">

**Author:** ![alexd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/96bed5/32.png) [@alexd](https://www.ruby-forum.com/u/alexd)\
**Post date:** [April 15, 2010, 10:36am UTC](https://www.ruby-forum.com/t/sort-collection-by-nested-model/186988/2 "2010-04-15T10:36:06Z")

</div>

On Apr 15, 8:44 am, Alex D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> @group_documents =
> 
> ```
> 
> GroupDocument.find\_all\_by\_group\_id(params[:group\_id]).paginate :page  
> =\> params[:page], :per\_page =\> 10, :order =\> ‘?’
> 
> Problem is with order. I want to return @group\_documents collection  
> ordered by document.title. Something like  
> :order =\> ‘group.title asc’

You need to join the groups table, then you’ll be able to order by  
groups.title

Fred

---

<div class="post-metadata">

**Author:** ![alexd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/96bed5/32.png) [@alexd](https://www.ruby-forum.com/u/alexd)\
**Post date:** [April 15, 2010, 10:43am UTC](https://www.ruby-forum.com/t/sort-collection-by-nested-model/186988/3 "2010-04-15T10:43:07Z")

</div>

Hi Fred,

thanks for replay.

Can you give me an example please, I’m not that good with queries.

Cheers Alex

On Apr 15, 6:34 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![alexd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/96bed5/32.png) [@alexd](https://www.ruby-forum.com/u/alexd)\
**Post date:** [April 15, 2010, 11:07am UTC](https://www.ruby-forum.com/t/sort-collection-by-nested-model/186988/4 "2010-04-15T11:07:42Z")

</div>

Hi Fred,

thanks for replay.

Can you give me an example please, I’m not that good with queries.

Cheers Alex

On Apr 15, 6:34 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![alexd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/96bed5/32.png) [@alexd](https://www.ruby-forum.com/u/alexd)\
**Post date:** [April 15, 2010, 10:57am UTC](https://www.ruby-forum.com/t/sort-collection-by-nested-model/186988/5 "2010-04-15T10:57:45Z")

</div>

Hi Fred,

thanks for replay.

Can you give me an example please, I’m not that good with queries.

Cheers Alex

On Apr 15, 6:34 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![alexd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/96bed5/32.png) [@alexd](https://www.ruby-forum.com/u/alexd)\
**Post date:** [April 15, 2010, 11:25am UTC](https://www.ruby-forum.com/t/sort-collection-by-nested-model/186988/6 "2010-04-15T11:25:50Z")

</div>

Hi Fred,

thanks for replay.

Can you give me an example please, I’m not that good with queries.

Cheers Alex

On Apr 15, 6:34 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![alexd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/96bed5/32.png) [@alexd](https://www.ruby-forum.com/u/alexd)\
**Post date:** [April 15, 2010, 4:27pm UTC](https://www.ruby-forum.com/t/sort-collection-by-nested-model/186988/7 "2010-04-15T16:27:23Z")

</div>

Alex D. wrote:

> Hi Fred,
> 
> thanks for replay.
> 
> Can you give me an example please, I’m not that good with queries.

An example won’t help you, then. What will help is reading about SQL  
joins and AR :joins.

> Cheers Alex
> 
> On Apr 15, 6:34ï¿½pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
