I''m a newbie and was curious about displaying scaffold views ascending or descending by date. Here''s what my current view looks like. The first column is the one I''d like to sort by so that the user can see each date in order as the event date is approaching. My controller is just the generated scaffold controller. I know that the default for Rails is to display each record by when it was created. How can I go about displaying this table by dates and what''s the best way to do it? Much thanks! <h1>Listing Dates</h1> <table> <tr> <th>Date</th> <th>Address1</th> <th>Address2</th> <th>City</th> <th>State</th> </tr> <% @dates.each do |date| %> <tr> <td><%=h date.date %></td> <td><%=h date.address1 %></td> <td><%=h date.address2 %></td> <td><%=h date.city %></td> <td><%=h date.state %></td> <td><%= link_to ''Show'', date %></td> <td><%= link_to ''Edit'', edit_date_path(date) %></td> <td><%= link_to ''Destroy'', date, :confirm => ''Are you sure?'', :method => :delete %></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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
This is more a Ruby problem. To sort an array (which is what I assume that @dates is) you need to use the Array#sort method <% @dates.sort{|a,b| a.date <=> b.date}.each do |date| %> That will probably put them in ascending order, for descending order switch the a and b round. However. It would be better if the data in @dates was in the correct order when it is collected by the controller. When you use Active Record to get data from the database you can get it to sort the data for you with, something like, :order => ''date''. You''ll have to look up the docs, I''ve not had my morning coffee yet It is a better practice to get the data in the right order when you get it from the database if you can, it''s not always possible. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Dubocit wrote:> I''m a newbie and was curious about displaying scaffold views ascending > or descending by date. Here''s what my current view looks like. The > first column is the one I''d like to sort by so that the user can see > each date in order as the event date is approaching. My controller is > just the generated scaffold controller. I know that the default for > Rails is to display each record by when it was created.This isn''t true. Rails has no default for sorting result sets. Rails just includes them in the order the query provides them.> How can I go about displaying this table by dates and what''s the best way to do it?Ask the database to put them in the order you want inside your controller action. It''s should the controller''s responsibility to fetch and organize the data to be presented not the view''s. def index @dates = DateModel.find(:all, :order => "date") end P.S. I hope did didn''t name your ActiveRecord model "Date" that might cause you problems in the future. P.P.S. I''d also recommend not naming a database column "date" either. You may run into naming conflicts and other issues as well. Rails has a convention for naming date and time related fields (i.e. created_at, updated_on). I''d recommend following that convention for your own date fields. Use *_on for date fields and *_at for datetime fields in order to avoid any possible naming conflicts. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks for all of the help! Like I said, I''m new with Ruby on Rails and many of the basics that I learned in tutorials were mostly Ruby code. I thought there was probably a good way to do this in the controller. So I''ve updated my controller to read: def index @events = current_user.events.find(:all, :order => "date") end Works perfectly with the scaffolding that''s in place. Thanks for the tip on the using "Date" as a model name. Luckily, I didn''t do that and only used it in my previous example. On Mar 10, 2:20 pm, Robert Walker <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Dubocit wrote: > > I''m a newbie and was curious about displaying scaffold views ascending > > or descending by date. Here''s what my current view looks like. The > > first column is the one I''d like to sort by so that the user can see > > each date in order as the event date is approaching. My controller is > > just the generated scaffold controller. I know that the default for > > Rails is to display each record by when it was created. > > This isn''t true. Rails has no default for sorting result sets. Rails > just includes them in the order the query provides them. > > > How can I go about displaying this table by dates and what''s the best way to do it? > > Ask the database to put them in the order you want inside your > controller action. It''s should the controller''s responsibility to fetch > and organize the data to be presented not the view''s. > > def index > @dates = DateModel.find(:all, :order => "date") > end > > P.S. I hope did didn''t name your ActiveRecord model "Date" that might > cause you problems in the future. > > P.P.S. I''d also recommend not naming a database column "date" either. > You may run into naming conflicts and other issues as well. Rails has a > convention for naming date and time related fields (i.e. created_at, > updated_on). I''d recommend following that convention for your own date > fields. Use *_on for date fields and *_at for datetime fields in order > to avoid any possible naming conflicts. > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.