4

I am doing a ruby on rails archive blog.The backend works fine but the year displays seperately as you can see in the below image

enter image description here

As you can see in the image above the years are coming seperately.But if I give some blog in the same month it works fine as you can see in the month of august 2014(accident,Politics) but not for 2014.

This is the controller

@posts_by_month = Cutting.find(:all, :order => "date DESC").group_by { |post| post.date.beginning_of_month }

the view

<div class="easy-tree" style="float:left;width:300px;background: #aaaaaa;">
   <%# @post_months.sort.reverse.each do |month, posts| %>
    <%@posts_by_month.each do |month, posts|%>
    <ul>
      <li><%=h month.strftime("%G")%>
        <ul>
          <li><%=h month.strftime("%B") %>
              <!--li ><%=h month.strftime %>-->
            <ul>
            <% for post in posts %>
              <li style="background: #aaaaaa;"class="getid" name ="<%=post.id%>"><%=h link_to post.subject%></li>
            <% end %>
                </ul>
              <!--/li-->            
          </li>
        </ul>
      </li>
    </ul>
    <% end %>
  </div>

I am using easy-tree for getting the ui for

Santino 'Sonny' Corleone
  • 1,735
  • 5
  • 25
  • 52

2 Answers2

2

Try this

 @posts = Cutting.all(:select => "id, subject, date, created_at", :order => "date DESC")
 @post_months = @posts.group_by { |t| t.created_at.beginning_of_year }

And on your views

<div class="easy-tree" style="float:left;width:300px;background: #aaaaaa;">
    <% @post_months.sort.reverse.each do |year, postss| %>
    <ul>
      <li><%=h year.strftime("%G")%>
        <% postss.group_by { |t| t.created_at.beginning_of_month }.each do |month, posts| %>
        <ul>
          <li><%=h month.strftime("%B") %>  
            <ul>
            <% for post in posts %>
              <li style="background: #aaaaaa;" class="getid" name ="<%=post.id%>"><%=h link_to post.subject %></li>
            <% end %>
                </ul>
           </li>
        </ul>
        <% end %>
      </li>
    </ul>
    <% end %>
  </div>

Output :

output

rails_id
  • 8,120
  • 4
  • 46
  • 84
  • This works thank you. I'm interested to know if it is considered bad design to put the nested group_by logic into the view. I was thinking about constructing a nested hash in the controller and then just iterating over that in the view. Would this be a best practice or is this overkill? – a2f0 May 19 '15 at 12:36
0

My hunch is your query is grouping the posts by month only - meaning you're going to receive an object back based on their month alone. Although this may show the year / month as you wish, the underlying structure of the object will be centered on months, rather than years (as you wish).

A way to test this will be to call @posts_by_month.debug in your view, allowing you to see exactly what the object looks like. I would surmise it will be just be sorted by months

--

Fix

The way to fix this will be group by the year and then by the month. I've never done this, but have researched & found the bottom line is you need to group by year and then by month, like this:

@posts_by_month = Post.all.order(date: :desc).group("year(created_at)").group("month(created_at)")

My example uses .group with reference this answer

Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147