So, I’m putting together a blog, and I want the posts on it to be easily
browsable by date. So far I’ve given each post attributes for the month
and year it was created. That way, when someone visits
example.com/blog/2008 Rails does a find_all_by_year and displays and
paginates the results - similarly, when someone visits
example.com/blog/2008/09 Rails does find_all_by_year_and_month and does
the same.
Now, I’m trying to change the blog layout so that it provides links to
each month as well as the post count for that month. This would look
something like:
August 2008 (24)
September 2008 (33)
October 2008 (17)
And so on. So, two questions:
#1 - What’s the best way to do this? The most direct method I see is to
do a Post.find_all_by_year_and_month(…).count for each month of each
year, but that would be pretty abusive of the database, right?
#2 - How do I do this and remain MVC? The above display is happening in
the layout, and I know I shouldn’t be getting very code-heavy in the
view, but I don’t know where else to put this kind of functionality.
Thanks!
Chris