Sort collection by nested model

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

On Apr 15, 8:44 am, Alex D. [email protected] 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

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. [email protected]

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. [email protected]

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. [email protected]

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. [email protected]

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. [email protected]

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs