I have the following models: Event name location customer_id with relation belongs_to :customer Person name address city type Customer < Person has_many :events Now I want to have a list, sorted on the name of the customer. This doesnt work Event.find(:all, :include => :customer, :order => customer.name) Or isnt this possible at all, just back to plain sql? Greetz, Adrie -- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
sure you can write in plain sql using find_by_sql method. but it is an easy query and it would easier doing it directly in rails code. I''m not sure i understand what you want maybe it is : Costumer.find :all, :include => :events, :order => ''customers.name'' and a question: maybe you made an errors by typing but why you wrote customer (singular) in ''customer.name'' ? On Jan 24, 1:31 am, Adrie Donker <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I have the following models: > Event > name > location > customer_id > with relation > belongs_to :customer > > Person > name > address > city > type > > Customer < Person > has_many :events > > Now I want to have a list, sorted on the name of the customer. > > This doesn''t work > Event.find(:all, :include => :customer, :order => ''customer.name'') > > Or isn''t this possible at all, just back to plain sql? > > Greetz, Adrie > -- > Posted viahttp://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 -~----------~----~----~----~------~----~------~--~---
Jean-sébastien Jney wrote:> Costumer.find :all, :include => :events, :order => ''customers.name''Jean, I want the Events (a selection on event.starttime) listed, but sorted bij de Customer''s name. Not all Events have a customer, so your query went give them. That why i tried: Event.find(:all, :include => :customer, :order => ''customer.name'') Anyway, thanks for your reply Adrie -- 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 24 Jan 2008, at 12:48, Adrie Donker wrote:> > > Jean-sébastien Jney wrote: >> Costumer.find :all, :include => :events, :order => ''customers.name'' > > Jean, > > I want the Events (a selection on event.starttime) listed, but sorted > bij de Customer''s name. Not all Events have a customer, so your query > went give them. > > That why i tried: > Event.find(:all, :include => :customer, :order => ''customer.name'')just add a condition: ''customers.id IS NOT NULL'' (for mysql, other dbs will have something equivalent''). There''s also :joins instead of :include (which does mostly the same joining, but doesn''t actually instantiate the customer objects) Fred> > > Anyway, thanks for your reply > Adrie > -- > 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> > just add a condition: ''customers.id IS NOT NULL'' (for mysql, other dbs > will have something equivalent''). > There''s also :joins instead of :include (which does mostly the same > joining, but doesn''t actually instantiate the customer objects) > > FredFred, I worked it out, this is the way: Event.find(:all, :include => :customer, :order => ''people.name'') The NULL clause doesn''t seem necessary, just use the plural name of the table, not the model. Thanks for the support Adrie -- 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 -~----------~----~----~----~------~----~------~--~---