Forum: Ruby on Rails Archive monthly count for blog

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0de83fd56f1af530034ba8efa5490b1b?d=identicon&s=25 John Butler (Guest)
on 2006-04-05 11:32
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?
0de83fd56f1af530034ba8efa5490b1b?d=identicon&s=25 John Butler (Guest)
on 2006-04-05 11:55
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
0de83fd56f1af530034ba8efa5490b1b?d=identicon&s=25 John Butler (Guest)
on 2006-04-05 17:08
Anybody point me in the right direction?

I'm really stuck on this!
0de83fd56f1af530034ba8efa5490b1b?d=identicon&s=25 John Butler (Guest)
on 2006-04-05 20:32
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)
0de83fd56f1af530034ba8efa5490b1b?d=identicon&s=25 John Butler (Guest)
on 2006-04-05 21:02
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 <month>(<noofposts>) 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...
22580f640e491f0920a192610d1df393?d=identicon&s=25 Tom Davies (Guest)
on 2006-04-05 22:02
(Received via mailing list)
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/Ba...

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 Butler <JohnnyButler7@gmail.com> wrote:
> from posts
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Tom Davies

http://blog.atomgiant.com
http://gifthat.com
0de83fd56f1af530034ba8efa5490b1b?d=identicon&s=25 John Butler (Guest)
on 2006-04-05 22:42
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 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
22580f640e491f0920a192610d1df393?d=identicon&s=25 Tom Davies (Guest)
on 2006-04-05 23:46
(Received via mailing list)
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/

Good luck convincing your boss.

Tom

On 4/5/06, John Butler <JohnnyButler7@gmail.com> wrote:
> We started on asp.net 2.0 but from researching ajax i kept coming
>
> JB
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Tom Davies

http://blog.atomgiant.com
http://gifthat.com
This topic is locked and can not be replied to.