Archive monthly count for blog

I am creating a blog to learn ruby on rails.

from the layout page i pass all the posts as a collection to

<%= render :partial => “archive”, :collection => @archive %>

On _archive.rhtml i have access to the collection. I am then gone to
render partial another page _archivecount.rhtml to display the number of
posts for each month.

Can anyone give a clue as how to to this in rubyonrails?

I have the def in the post model to return the number of posts for a
month but dont know how to loop through each month in the posts table

def self.month_posts
:conditions => “month(created_at) = 3”,
:order => “created_at desc”)

Anybody point me in the right direction?

I’m really stuck on this!

I have the SQL but dont know how to use inside rails?

Below will give me a count of the posts per month/year

select count(,monthname(created_at),year(created_at)
from posts
group by month(created_at), year(created_at)

Below will give me the posts
select *
from posts
group by month(created_at), year(created_at)

Ive got this in the post model which will return all the posts i want

def self.posts_months_count()
:group => " month(created_at), year(created_at)")

And this in the layout to display them
<%= render :partial => “archive”, :collection => @archives %>

How can i output them in a () format e.g. April
2006(4) etc

I also have this code in post which will return the posts for a
particula month year but how do i display all the months and number of
posts together

def self.month_posts(monthid, yearid)
:conditions => “month(created_at) = #{monthid} AND year(created_at)
= #{yearid}”,
:order => “created_at asc”)

So what i want to display is all the months/year and when the user
clicks a montht, all the posts for that month will appear.

January 2005(5)
February 2005(6)

Hi John,

You can achieve what you want by passing options to the
ActiveRecord.find method.

Here is a link to the documentation:

Here is an untested example that should get you what you want:

@posts = Post.find(:all, :select => ‘count(id) as cnt,
monthname(created_at) as month, year(created_at) as year’, :group =>
‘month(created_at), year(created_at)’)

Then to grab the data from each post:

@posts.each do |post|


On 4/5/06, John B. [email protected] wrote:

from posts

Tom D.

Thanks tom,

I knew what i wanted to do and sort of how to do it but just needed an
example. I’m new to ruby on rails and web development in general.

My background is Java, C# and our company have recently started web
development because the hype around ajax means we can offer more
sophisticated web systems to perform like desktop without the deployment

We started on 2.0 but from researching ajax i kept coming
accross ruby on rails.

The boss said if i can show him that ruby on rails is a real alternative
then we can devote more time to it. I am really impressed with it so
far especially being able to reuse templates/partials and the MVC stuff
i am really comfortable with. The only down side is the tools
available. I am using radrails with eclipse but debugging and the TDD is
proving difficult but i suppose over time these will improve!

Thanks again


Hi John,

Yeah, I personally don’t do much debugging in RadRails… but you will
find that most of what you want is in the log or you should be adding
into the log.

I also come from a Java background. I started with Rails before
RadRails existed and I must say things are much better with it, so
hang in there.

Also, if you aren’t using it, I highly recommend the RI view in
RadRails. It is a great way to become familiar with the Ruby synax.
Also, if you use firefox, the DevBoi firefox plugin has an addon pack
for Rails that is excellent (although it is still only for 1.0):

Good luck convincing your boss.


On 4/5/06, John B. [email protected] wrote:

We started on 2.0 but from researching ajax i kept coming


Posted via

Rails mailing list
[email protected]

Tom D.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs