I have a model, Day, and the table, ‘days’, contains 10,000 day records
each with a field, ‘date’, which has a ruby date object matched to a
mysql date type, YYYY-MM-DD. I would like my controller to paginate Day
by day.date.year. How do I do this? In other words, on each page, I
want EITHER 365 records or 366 depending on whether or not it is a leap
year–I want all of the days for a single year displayed on each page.
I did this recently to paginate by month. Wasn’t too hard as I recall.
Just put the year into the route
/:controller/:action/:year
Or something similar. Then you can do: Day.find_by_year(params[:year])
Then just create links to each year, or what I did is something like
<%= link_to :action => thesameaction, :year => params[:year] + 1 %>
I didn’t do the plus 1 thing, I did next_month or something. But it’s
doable.
I added “map.connect ‘:controller/:action/:year’” to the routes, but I
can’t get a find_by_year method in the Day model. Year is not a field
in the table, date is. The year is accessed through date.year. How do
I create a model variable so that I can create a year class variable and
ruby will then create for me a find_by_year method for the model???
You have to edit config/routes.rb.
Read the section on routing in AWDWR. This is one of the sections that
made me go “wow”.
You need to know it if you’re going to develop in Rails.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.