Hello, I am very new to RoR and am learning at a fairly good pace, but a seemingly simple task is leaving me stuck. I have a DB table called ''workers'' and fields for ''first_name'' and ''last_name'' need to be combined in the ''list'' view and shown in order of last name. The code I have below will render the name as "Last, First". How can I order the table by the name column when it is a combination of 2 fields? --thanks <table class="list" width="90%"> <tr> <th>Name</th> <th>Email</th> <th>Phone Number #1</th> <th>Phone Number #2</th> <th>Options</th> </tr> <% for worker in @workers %> <% row_color = cycle("F8F8F8", "ffffff")%> <tr style="background: #<%= row_color %>;"> <td><%= link_to %Q{ #{worker.last_name}, #{worker.first_name}}, :action => ''show'', :id => worker %></td> <td><%= mail_to worker.email, worker.email, :subject => "This is an example email", :body => "This is the body of the message." %></ td> <td><%= worker.telephone_1 %></td> <td><%= worker.telephone_2 %></td> <td><span class="smaller"><%= link_to ''Edit'', :action => ''edit'', :id => worker %> <%= link_to ''Delete'', { :action => ''destroy'', :id => worker }, :confirm => ''Are you sure?'', :post => true %></span></td> </tr> <% end %> </table> --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi, I''m pretty new to RoR, too. But for what it''s worth, I think you need to look at your controller which populates your @workers collection and add an :order expression, something like: find(:all, order="name") If nothing else, look up "order" in a Rails book. HTH, Richard On Feb 11, 6:02 pm, "ebrad" <nisguy_...-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> Hello, > I am very new to RoR and am learning at a fairly good pace, but a > seemingly simple task is leaving me stuck. I have a DB table called > ''workers'' and fields for ''first_name'' and ''last_name'' need to be > combined in the ''list'' view and shown in order of last name. The code > I have below will render the name as "Last, First". How can I order > the table by the name column when it is a combination of 2 fields? > > --thanks > > <table class="list" width="90%"> > <tr> > <th>Name</th> > <th>Email</th> > <th>Phone Number #1</th> > <th>Phone Number #2</th> > <th>Options</th> > </tr> > <% for worker in @workers %> > <% row_color = cycle("F8F8F8", "ffffff")%> > <tr style="background: #<%= row_color %>;"> > <td><%= link_to %Q{ #{worker.last_name}, > #{worker.first_name}}, :action => ''show'', :id => worker %></td> > <td><%= mail_to worker.email, worker.email, :subject => "This is > an example email", :body => "This is the body of the message." %></ > td> > <td><%= worker.telephone_1 %></td> > <td><%= worker.telephone_2 %></td> > <td><span class="smaller"><%= link_to ''Edit'', :action => > ''edit'', :id => worker %> > <%= link_to ''Delete'', { :action => ''destroy'', :id => > worker }, :confirm => ''Are you sure?'', :post => true %></span></td> > </tr> > <% end %> > </table>--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Richard wrote:> Hi, > > I''m pretty new to RoR, too. But for what it''s worth, I think you need > to look at your controller which populates your @workers collection > and add an :order expression, something like: > > find(:all, order="name") > > If nothing else, look up "order" in a Rails book. > > HTH, > RichardRight idea. You can pass an :order parameter to Model.find method that is basically an SQL fragment for setting the order of the results. You should be fetching these workers in your controller, so change that line of code to something like: @workers = Worker.find(:all, :order => ''last_name, first_name'') Which would generate SQL like: SELECT * FROM workers ORDER BY last_name, first_name This way last_name is the primary sort, and first_name is the secondary sort in case of ties. -- 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 -~----------~----~----~----~------~----~------~--~---
Hi Wayne,> @workers = Worker.find(:all, :order => ''last_name, first_name'')Nice job. But do you know where there''s an example of this where the app toggles between ASC and DESC when the column-name is clicked? I got some suggestions about this a few months ago but never got it working right. I''d like to find an "applet" on the Web or in a book that demonstrates this. Regards, Richard On Feb 11, 7:20 pm, Alex Wayne <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Richard wrote: > > Hi, > > > I''m pretty new to RoR, too. But for what it''s worth, I think you need > > to look at your controller which populates your @workers collection > > and add an :order expression, something like: > > > find(:all, order="name") > > > If nothing else, look up "order" in a Rails book. > > > HTH, > > Richard > > Right idea. You can pass an :order parameter to Model.find method that > is basically an SQL fragment for setting the order of the results. > > You should be fetching these workers in your controller, so change that > line of code to something like: > > @workers = Worker.find(:all, :order => ''last_name, first_name'') > > Which would generate SQL like: > > SELECT * FROM workers ORDER BY last_name, first_name > > This way last_name is the primary sort, and first_name is the secondary > sort in case of ties. > > -- > 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 -~----------~----~----~----~------~----~------~--~---
Try this. http://www.kryogenix.org/code/browser/sorttable/ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi, Thanks for that GREAT article. Of course, it''s all JavaScript rather than Ruby/Rails. But it works nicely and is designed beautifully, so it''ll be a great education for me to translate it to RoR with minimal JavaScript. Best wishes, Richard On Mar 4, 6:37 pm, "jko170" <jko...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Try this.http://www.kryogenix.org/code/browser/sorttable/--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I ended up using that too. It''s a great tool! On Mar 15, 2:38 am, "Richard" <RichardDummyMailbox58...-FtJgd9dCuO3JTKoYRCtP1UEOCMrvLtNR@public.gmane.org> wrote:> Hi, > > Thanks for that GREAT article. Of course, it''s all JavaScript rather > than Ruby/Rails. But it works nicely and is designed beautifully, so > it''ll be a great education for me to translate it to RoR with minimal > JavaScript. > > Best wishes, > Richard > > On Mar 4, 6:37 pm, "jko170" <jko...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Try this.http://www.kryogenix.org/code/browser/sorttable/- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I forgot to thank you for this. It was helpful! On Feb 11, 8:20 pm, Alex Wayne <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Richard wrote: > > Hi, > > > I''m pretty new to RoR, too. But for what it''s worth, I think you need > > to look at your controller which populates your @workers collection > > and add an :order expression, something like: > > > find(:all, order="name") > > > If nothing else, look up "order" in a Rails book. > > > HTH, > > Richard > > Right idea. You can pass an :order parameter to Model.find method that > is basically an SQL fragment for setting the order of the results. > > You should be fetching these workers in your controller, so change that > line of code to something like: > > @workers = Worker.find(:all, :order => ''last_name, first_name'') > > Which would generate SQL like: > > SELECT * FROM workers ORDER BY last_name, first_name > > This way last_name is the primary sort, and first_name is the secondary > sort in case of ties. > > -- > 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 -~----------~----~----~----~------~----~------~--~---