# Archive monthly count for blog

**URL:** <https://www.ruby-forum.com/t/archive-monthly-count-for-blog/93636>\
**Category:** Rails\
**Created:** [April 5, 2006, 11:32am UTC](https://www.ruby-forum.com/t/archive-monthly-count-for-blog/93636 "2006-04-05T11:32:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 5, 2006, 11:32am UTC](https://www.ruby-forum.com/t/archive-monthly-count-for-blog/93636/1 "2006-04-05T11:32:47Z")

</div>

I am creating a blog to learn ruby on rails.

from the layout page i pass all the posts as a collection to  
\_archive.rhtml

\<%= 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?

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 5, 2006, 11:55am UTC](https://www.ruby-forum.com/t/archive-monthly-count-for-blog/93636/2 "2006-04-05T11:55:32Z")

</div>

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  
find(:all,  
:conditions =\> “month(created\_at) = 3”,  
:order =\> “created\_at desc”)  
end

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 5, 2006, 5:08pm UTC](https://www.ruby-forum.com/t/archive-monthly-count-for-blog/93636/3 "2006-04-05T17:08:41Z")

</div>

Anybody point me in the right direction?

I’m really stuck on this!

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 5, 2006, 8:32pm UTC](https://www.ruby-forum.com/t/archive-monthly-count-for-blog/93636/4 "2006-04-05T20:32:47Z")

</div>

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(posts.id),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)

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 5, 2006, 9:02pm UTC](https://www.ruby-forum.com/t/archive-monthly-count-for-blog/93636/5 "2006-04-05T21:02:56Z")

</div>

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

def self.posts\_months\_count()  
find(:all,  
:group =\> " month(created\_at), year(created\_at)")  
end

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)  
find(:all,  
:conditions =\> “month(created\_at) = #{monthid} AND year(created\_at)  
= #{yearid}”,  
:order =\> “created\_at asc”)  
end

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)  
etc…

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 5, 2006, 10:02pm UTC](https://www.ruby-forum.com/t/archive-monthly-count-for-blog/93636/6 "2006-04-05T22:02:20Z")

</div>

Hi John,

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

Here is a link to the documentation:  
[http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M000855](http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M000855)

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|  
post.cnt  
post.month  
post.year  
end

Tom

On 4/5/06, John B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> from posts  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Tom D.

[http://blog.atomgiant.com](http://blog.atomgiant.com)  
[http://gifthat.com](http://gifthat.com)

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 5, 2006, 10:42pm UTC](https://www.ruby-forum.com/t/archive-monthly-count-for-blog/93636/7 "2006-04-05T22:42:45Z")

</div>

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  
issues.

We started on [asp.net](http://asp.net) 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

JB

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [April 5, 2006, 11:46pm UTC](https://www.ruby-forum.com/t/archive-monthly-count-for-blog/93636/8 "2006-04-05T23:46:40Z")

</div>

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):

[http://www.martincohen.info/products/devboi/](http://www.martincohen.info/products/devboi/)

Good luck convincing your boss.

Tom

On 4/5/06, John B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> We started on [asp.net](http://asp.net) 2.0 but from researching ajax i kept coming
> 
> JB
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Tom D.

[http://blog.atomgiant.com](http://blog.atomgiant.com)  
[http://gifthat.com](http://gifthat.com)
