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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Franz Strebel
2008-Dec-21 19:24 UTC
Re: How to order by a field in the belongs_to association
On Sun, Dec 21, 2008 at 7:34 PM, Patrick Doyle <wpdster-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 haveSpecify the :order param in your find, à la :order => ''documents.number ASC'' --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Patrick Doyle
2008-Dec-21 19:40 UTC
Re: How to order by a field in the belongs_to association
On Sun, Dec 21, 2008 at 2:24 PM, Franz Strebel <franz.strebel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> > On Sun, Dec 21, 2008 at 7:34 PM, Patrick Doyle <wpdster-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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'' > > I can''t figure out how to make that work -- the revisions table does nothave 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Franz Strebel
2008-Dec-21 20:56 UTC
Re: How to order by a field in the belongs_to association
On Sun, Dec 21, 2008 at 8:40 PM, Patrick Doyle <wpdster-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Patrick Doyle
2008-Dec-21 22:40 UTC
Re: How to order by a field in the belongs_to association
> 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---