hey there - given a simple table like this: orders.id orders.client_id orders.supplier_id orders.date_ordered I create and index view that has in it something like this: <td><%= @orders.id %></td> <td><%= @orders.client_name %></td> <td><%= @orders.supplier_name %></td> <td><%= @orders.date_ordered %></td> Ultimately, I''m wanting to provide an ''index'' page, that can be resorted on a column of the users choice (i.e.id, client_name, suplier_name, date_ordered) So thats obviously easy for date_ordered, and id i.e. Order.find(:all, :order=>"date_ordered") for example BUT to sort by the the client name or supplier name, data which are obviously in a joined table - it gets curlier. Coming from an sql background, i''d assume it should be very very simple, but activerecord seems to obscure something simple and make it a guessing game of syntax - ok - probably not a fair comment... can someone set me straight on this and show me how ? thanks glenn --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
should just work like: @orders = Order.find :all, :include => :suppliers, :order => "supplier.name DESC" the value to pass to :order is just an SQL Fragment. in SQL you would do "ORDER BY supplier.name DESC" in AR you do :order => "supplier.name DESC" On 10 Apr., 10:13, "glenn" <g...-H7U4i/pZ250d9SLi6J12ItHuzzzSOjJt@public.gmane.org> wrote:> hey there - > > given a simple table like this: > orders.id > orders.client_id > orders.supplier_id > orders.date_ordered > > I create and index view that has in it something like this: > <td><%= @orders.id %></td> > <td><%= @orders.client_name %></td> > <td><%= @orders.supplier_name %></td> > <td><%= @orders.date_ordered %></td> > > Ultimately, I''m wanting to provide an ''index'' page, that can be > resorted on a column of the users choice (i.e.id, client_name, > suplier_name, date_ordered) > > So thats obviously easy for date_ordered, and id > i.e. > Order.find(:all, :order=>"date_ordered") for example > > BUT to sort by the the client name or supplier name, data which are > obviously in a joined table - it gets curlier. Coming from an sql > background, i''d assume it should be very very simple, but activerecord > seems to obscure something simple and make it a guessing game of > syntax - ok - probably not a fair comment... can someone set me > straight on this and show me how ? > > thanks > > glenn--~--~---------~--~----~------------~-------~--~----~ 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 Apr 10, 6:52 pm, "Thorsten" <duple...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> should just work like: > > @orders = Order.find :all, :include => :suppliers, :order => > "supplier.name DESC" > > the value to pass to :order is just an SQL Fragment. > in SQL you would do "ORDER BY supplier.name DESC" > in AR you do :order => "supplier.name DESC" >Beautiful... thanks - like I said, prob wasnt a fair comment ;-) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ball, Donald A Jr \(Library\)
2007-Apr-10 19:00 UTC
Re: easiest way to order by field in joined table
> given a simple table like this: > orders.id > orders.client_id > orders.supplier_id > orders.date_ordered > > I create and index view that has in it something like this: > <td><%= @orders.id %></td> > <td><%= @orders.client_name %></td> > <td><%= @orders.supplier_name %></td> > <td><%= @orders.date_ordered %></td> > > Ultimately, I''m wanting to provide an ''index'' page, that can > be resorted on a column of the users choice (i.e.id, > client_name, suplier_name, date_ordered) > > So thats obviously easy for date_ordered, and id i.e. > Order.find(:all, :order=>"date_ordered") for example > > > BUT to sort by the the client name or supplier name, data > which are obviously in a joined table - it gets curlier. > Coming from an sql background, i''d assume it should be very > very simple, but activerecord seems to obscure something > simple and make it a guessing game of syntax - ok - probably > not a fair comment... can someone set me straight on this and > show me how ?Try... Order.find(:all, {:include=>[:client, :supplier], :order=>''clients.name''}) - donald --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---