On 28/05/2009, at 11:06 AM, Chris Hanks wrote:
>
> Ok, after some thinking and experimentation, I came up with:
>
> <% posts = Post.all(:select => "year, month", :order => "year,
> month").collect(&:attributes) %>
Any time you're doing equalities against a model object (here Post),
you could put it in the controller and assign it to an instance
varaible.
Also I'm not sure why you're getting the attributes of the objects,
when you could be getting the objects by themselves.
controller:
@posts = Post.all(:select => "year, month", :order => "year DESC,
month DESC")
view:
<% @posts.group_by(&:year).each do |year, year_of_posts | %>
<%= year %>
<% year_of_posts.group_by(&:month) do |month, month_of_posts| %>
<% month_of_posts.each do |post| %>
<%= "some data about the posts or links or whatever %>
<% end %>
<% end %>
<% end %>
obviously you should change this to be more in line with what you
actually want (probably the inner each is unnecessary for your
requirements.
Rather than reversing in ruby, use order with DESC sql fragment to get
the data the way you want it from the database. This is what the
database is good at.
Also, have you thought of actually storing a datetime for the post,
instead of doing all this crazy number stuff? Then you could use the
strftime method on the datetime to print out the dates nicely. Much
more elegant.
Julian.
----------------------------------------------
Learn: http://sensei.zenunit.com/
Last updated 20-May-09 (Rails, Basic Unix)
Blog: http://random8.zenunit.com/
Twitter: http://twitter.com/random8r
> <% (posts.first["year"]..posts.last["year"]).to_a.reverse.each do |
> year|
> %>
> <p><%= year %></p>
> <% posts_by_year = posts.select{|hash| hash["year"] == year} %>
> <ul class="nostyle">
> <% (1..12).to_a.reverse.each do |index| %>
> <% posts_by_month = posts_by_year.select{|hash| hash["month"] ==
> ("%02d" % index)}%>
> <% unless posts_by_month.length == 0 %>
> <li><%= link_to "#{Date::MONTHNAMES[index]} #{year}",
> month_path(year, ("%02d" % index)) %> (<%= posts_by_month.length
> %>)</li>
> <% end %>
> <% end %>
> </ul>
> <% end %>
>
> It's working well. It's all still stuck in the view, but oh well. If
> anyone knows a better place, let me know.
> --
> 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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---