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

@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

Hi Fred,

thanks for replay.

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

Cheers Alex

Hi Fred,

thanks for replay.

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

Cheers Alex

Hi Fred,

thanks for replay.

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

Cheers Alex

Alex Djioev 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 Cheung <frederick.che...@gmail.com>

Best,