Hello. I have what i assume is a simple problem. I have want to be able to sort a dataset by referencing the text name of a related class. For example: let''s pretend I have these models: "things" and "colors". Things "belongs_to" Colors and Colors "has_one" things. Thing.color_id = Color.id So, Thing.color.name = Color.name where Thing.color_id == Color.id You get it. So, I want to do this: mythings = Thing.find(:all, :order => *on Thing.color.name*) I want to be able to sort all my things by the colors (red, blue, etc) by name and not their ID in the database. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Try this: mythings = Thing.find(:all, :include => :color, :order => ''colors.name asc'') -- Josh Nick Norton wrote:> Hello. I have what i assume is a simple problem. I have want to be able > to sort a dataset by referencing the text name of a related class. > > For example: let''s pretend I have these models: "things" and "colors". > > Things "belongs_to" Colors and Colors "has_one" things. > > Thing.color_id = Color.id > > So, Thing.color.name = Color.name where Thing.color_id == Color.id > > You get it. > > So, I want to do this: > > mythings = Thing.find(:all, :order => *on Thing.color.name*) > > I want to be able to sort all my things by the colors (red, blue, etc) > by name and not their ID in the database.-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 28 Aug 2008, at 03:27, Joshua Abbott wrote:> > Try this: > > mythings = Thing.find(:all, :include => :color, :order => ''colors.name > asc'') >That will work, but really all you need is :joins. You just need to join the colours table, not do all the instantiating of the extra objects. Fred> -- Josh > > Nick Norton wrote: >> Hello. I have what i assume is a simple problem. I have want to be >> able >> to sort a dataset by referencing the text name of a related class. >> >> For example: let''s pretend I have these models: "things" and >> "colors". >> >> Things "belongs_to" Colors and Colors "has_one" things. >> >> Thing.color_id = Color.id >> >> So, Thing.color.name = Color.name where Thing.color_id == Color.id >> >> You get it. >> >> So, I want to do this: >> >> mythings = Thing.find(:all, :order => *on Thing.color.name*) >> >> I want to be able to sort all my things by the colors (red, blue, >> etc) >> by name and not their ID in the database. > > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---