How to auto-generate a month-by-month archive links?

Hey there,

Does anyone know how best to create a list of links that will sort
objects by the month in which they were created, and automatically add
months as time goes on?

I have a method that can find the objects created on a certain day,
month, or year, based on url parameters. My question is how to
automatically generate the links that will pass my method the correct
parameters, based on the current date, and stopping listing at some
arbitrary date in the past (say May 2006).

I’m trying to mimic a typical blog-style archive, so if someone can
advise for an easier way to do that, that would be cool, too.

Thanks very much!

rg wrote:

Hey there,

Does anyone know how best to create a list of links that will sort
objects by the month in which they were created, and automatically add
months as time goes on?

I have a method that can find the objects created on a certain day,
month, or year, based on url parameters. My question is how to
automatically generate the links that will pass my method the correct
parameters, based on the current date, and stopping listing at some
arbitrary date in the past (say May 2006).

I’m trying to mimic a typical blog-style archive, so if someone can
advise for an easier way to do that, that would be cool, too.

Thanks very much!

Did you mean something like this ?

controller:
@pages = Page.find(:all, :order => “created_on”);

helper:
def time_to_link_year_and_month(date)
Time.parse(date).year.to_s + “/” + Time.parse(date).month.to_s + “/”
end

Then you can use it in your link_to

@pages.each do | page |

link_to “Title”, “blog/archive” +
time_to_link_year_and_month(@page.date)

end

Thanks for the reply.

That’s not exactly what I need. I’d like a list of link such as this:

August 2007
July 2007
June 2007

to be generated automatically, depending on the current date.
For instance, once October is here, a link titled ‘September 2007’
will be added to the top of the list.

Each link in this list should simply have a URL that reflects its year
and month, e.g.

/blog/archive/2007/09

, the parameters of which (year and month) I can handle in a
controller method that finds posted in a given month, based on the
‘created_at’ date value.

So, instead of looping over the pages, as in your example, I’d like to
loop over the months since an arbitrary month in the past, e.g. June
2006.

Know what I mean?

Thanks.

On Sep 6, 10:54 pm, Jamal S. [email protected]

Something like this ?

def self.find_and_group_by_year_month
find(:all, :select => “DATE_FORMAT(created_at, ‘%m’) as month,
COUNT(posts.id) as count”, :group => “month”, :order => “month DESC”)
end

then you can loop through them?

I’m not totally sure, to be honest. What would the corresponding view
code look like?

To perhaps give you a better idea of what I’m after, take a look at
the archive links in the right sidebar of this blog:
http://www.robbyonrails.com/

Thanks for the help. I really appreciate it!

On Sep 7, 12:14 am, Jamal S. [email protected]

rg wrote:

I’m not totally sure, to be honest. What would the corresponding view
code look like?

To perhaps give you a better idea of what I’m after, take a look at
the archive links in the right sidebar of this blog:
http://www.robbyonrails.com/

Thanks for the help. I really appreciate it!

On Sep 7, 12:14 am, Jamal S. [email protected]

My code will archive this:

* September 2007 (1)
* August 2007 (10)
* July 2007 (5)
* June 2007 (10)
* May 2007 (12)
* April 2007 (8)
* March 2007 (10)
* February 2007 (18)

The code view will look like this :slight_smile:

<% @archives.each do |archive| %>
<%= Time.parse(archive.month+ “/ 2007”).strftime(“%B”) %> (<%=
archive.count %>)
<% end %>

You gotta change it so it fits your needs :slight_smile:

OK, I’ve almost got it perfect.

I modified your code to also extract the year of the archive (your
code works fine without this modification):

@archives = Post.find(:all, :select => “DATE_FORMAT(created_at, ‘%Y’)
as year, DATE_FORMAT(created_at, ‘%m’) as month, COUNT(posts.id) as
count”, :group => “month”, :order => “month DESC”)
end

and display it with this

<% @archives.each do |archive| %>
<%= link_to Time.parse(archive.month+ “/ 2007”).strftime(“%B”),
“archive/”+archive.year+“/”+archive.month %> (<%=
archive.count %>)
<% end %>

But I get a NoMethodError on ‘year’ in the view. What am I doing
wrong in adding the year?

Thanks.

On Sep 7, 8:51 am, Jamal S. [email protected]

That’s weird, it suppose to work as month did :slight_smile:

I cannot think of any other options?

Sounds good :slight_smile:

Can you tell me the solution, thanks in advance.

I figured out my problem, and it works perfectly. Thanks Jamal!

Why not custom routes, and group_by?

See

On 9/7/07, rg [email protected] wrote:

parameters, based on the current date, and stopping listing at some
arbitrary date in the past (say May 2006).

I’m trying to mimic a typical blog-style archive, so if someone can
advise for an easier way to do that, that would be cool, too.

Thanks very much!


Ramon T.