Forum: Ruby on Rails How to order by a field in the belongs_to association

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ed437e52d8d6720308720e7e678f3e6d?d=identicon&s=25 Patrick Doyle (Guest)
on 2008-12-21 19:35
(Received via mailing list)
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
9b8d00f99fe595bdc45fd30439cf9c8f?d=identicon&s=25 Franz Strebel (Guest)
on 2008-12-21 20:25
(Received via mailing list)
On Sun, Dec 21, 2008 at 7:34 PM, Patrick Doyle <wpdster@gmail.com>
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'
Ed437e52d8d6720308720e7e678f3e6d?d=identicon&s=25 Patrick Doyle (Guest)
on 2008-12-21 20:41
(Received via mailing list)
On Sun, Dec 21, 2008 at 2:24 PM, Franz Strebel
<franz.strebel@gmail.com>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
9b8d00f99fe595bdc45fd30439cf9c8f?d=identicon&s=25 Franz Strebel (Guest)
on 2008-12-21 21:56
(Received via mailing list)
On Sun, Dec 21, 2008 at 8:40 PM, Patrick Doyle <wpdster@gmail.com>
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
Ed437e52d8d6720308720e7e678f3e6d?d=identicon&s=25 Patrick Doyle (Guest)
on 2008-12-21 23:41
(Received via mailing list)
> 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
This topic is locked and can not be replied to.