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! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jamal Soueidan
2007-Sep-07 05:54 UTC
Re: how to auto-generate a month-by-month archive links?
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jamal Soueidan
2007-Sep-07 07:14 UTC
Re: how to auto-generate a month-by-month archive links?
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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Soueidan <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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? > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jamal Soueidan
2007-Sep-07 15:51 UTC
Re: how to auto-generate a month-by-month archive links?
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 Soueidan <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>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 :) <% @archives.each do |archive| %> <%= Time.parse(archive.month+ "/ 2007").strftime("%B") %> (<%= archive.count %>) <% end %> You gotta change it so it fits your needs :) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Soueidan <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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 Soueidan <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > > 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 :) > > <% @archives.each do |archive| %> > <%= Time.parse(archive.month+ "/ 2007").strftime("%B") %> (<%> archive.count %>) > <% end %> > > You gotta change it so it fits your needs :) > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jamal Soueidan
2007-Sep-07 17:30 UTC
Re: how to auto-generate a month-by-month archive links?
That''s weird, it suppose to work as month did :) I cannot think of any other options? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
So, my additional SQL syntax for ''year'' is correct, right? Don''t worry about it, though. You''ve been very helpful. Thanks again. On Sep 7, 10:30 am, Jamal Soueidan <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> That''s weird, it suppose to work as month did :) > > I cannot think of any other options? > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I figured out my problem, and it works perfectly. Thanks Jamal! On Sep 7, 12:09 pm, rg <rohi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> So, my additional SQL syntax for ''year'' is correct, right? > > Don''t worry about it, though. You''ve been very helpful. Thanks > again. > > On Sep 7, 10:30 am, Jamal Soueidan <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > That''s weird, it suppose to work as month did :) > > > I cannot think of any other options? > > -- > > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jamal Soueidan
2007-Sep-26 06:16 UTC
Re: how to auto-generate a month-by-month archive links?
Sounds good :) Can you tell me the solution, thanks in advance. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ramon Miguel M. Tayag
2007-Sep-26 07:20 UTC
Re: how to auto-generate a month-by-month archive links?
Why not custom routes, and group_by? See http://railscasts.com/episodes/70 http://railscasts.com/episodes/29 On 9/7/07, rg <rohitkg-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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!-- Ramon Tayag --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---