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!
Listing Dates
Date |
Address1 |
Address2 |
City |
State |
<% @dates.each do |date| %>
<%=h date.date %> |
<%=h date.address1 %> |
<%=h date.address2 %> |
<%=h date.city %> |
<%=h date.state %> |
<%= link_to 'Show', date %> |
<%= link_to 'Edit', edit_date_path(date) %> |
<%= link_to 'Destroy', date, :confirm => 'Are you
sure?', :method => :delete %> |
<% end %>
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.
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.
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.