# Paginate by Year

**URL:** <https://www.ruby-forum.com/t/paginate-by-year/73018>\
**Category:** Rails\
**Created:** [October 20, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/paginate-by-year/73018 "2006-10-20T18:02:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@sweb](https://www.ruby-forum.com/u/sweb)\
**Post date:** [October 20, 2006, 6:02pm UTC](https://www.ruby-forum.com/t/paginate-by-year/73018/1 "2006-10-20T18:02:47Z")

</div>

Hi,

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.

Any idea how to accomplish this?

SHW

---

<div class="post-metadata">

**Author:** ![sweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@sweb](https://www.ruby-forum.com/u/sweb)\
**Post date:** [October 20, 2006, 8:18pm UTC](https://www.ruby-forum.com/t/paginate-by-year/73018/2 "2006-10-20T20:18:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@sweb](https://www.ruby-forum.com/u/sweb)\
**Post date:** [October 20, 2006, 10:15pm UTC](https://www.ruby-forum.com/t/paginate-by-year/73018/3 "2006-10-20T22:15:21Z")

</div>

Can somebody explain this explanation to me? What does it mean to “add  
to route”.

Sam

---

<div class="post-metadata">

**Author:** ![sweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@sweb](https://www.ruby-forum.com/u/sweb)\
**Post date:** [October 22, 2006, 3:21am UTC](https://www.ruby-forum.com/t/paginate-by-year/73018/4 "2006-10-22T03:21:46Z")

</div>

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???

---

<div class="post-metadata">

**Author:** ![sweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@sweb](https://www.ruby-forum.com/u/sweb)\
**Post date:** [October 20, 2006, 10:53pm UTC](https://www.ruby-forum.com/t/paginate-by-year/73018/5 "2006-10-20T22:53:35Z")

</div>

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.
