I have an app that lists a bands shows in an unordered list on the home page.
<ul>
<% for show in @shows %>
<li>
<strong><%= link_to show.venue, show %></strong><br />
<%= display_date(show.show_date) %>
</li>
<% end %>
</ul>
There is a shows controller that handles creating the shows. It has rows in the database like venue, show_date, show_time, and so on. The home page of the app comes from a pages controller. The pages controller has two actions that create the home page and the about page.
class PagesController < ApplicationController
def home
@shows = Show.find(:all, :order => :show_date)
end
def about
end
end
The problem I’m having is that I only want the shows with a show_date greater than or equal to Date.today to be populated into the @shows instance variable that will in turn be populated into the unordered list. Any help writing a custom finder method would be greatly appreciated. Would it be easier to filter through the @shows variable using a helper method?
Here’s how to filter out dates (this uses Rails 3’s newer
where(...)syntax instead offind(:all...)).Also, in your
Showmodel you could add a named scope so you can reuse this in other places:Then in your controller, you could just do: