3

<h1> What it looks like currently</h1><br>

<table>
 <thead>
    <tr>
      <th>Month</th>
      <th>Month Total</th>
      <th>Written</th>
      <th>Verbal</th>
      <th>Probable 75%</th>
      <th>Probable 25%</th>
      <th>Speculative</th>
      <th colspan="3"></th>
    </tr>
  </thead>
  <tbody>
  <tr><td>Jan 2017</td> <td>50</td><td>0</td><td>50</td><td>0</td><td>0</td><td>0</td></tr>
 <tr> <td>Feb 2017</td> <td>100</td><td>0</td><td>100</td><td>0</td><td>0</td><td>0</td></tr>
  <tr><td>Mar 2017</td>  <td>700</td><td>0</td><td>700</td><td>0</td><td>0</td><td>0</td></tr>
  <tr><td>Jan 2017</td>  <td>700</td><td>700</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>
 <tr><td>Feb 2017</td>  <td>5000</td><td>5000</td><td>0</td><td>0</td><td>0</td><td>0</td></tr>
  <tr><td>Jan 2017</td> <td>500</td><td>0</td><td>0</td><td>500</td><td>0</td><td>0</td></tr>
  <tr><td>Jan 2017</td> <td>5000</td><td>0</td><td>0</td><td>0</td><td>0</td><td>5000</td></tr>
  </tbody>
  </table>
  
  <h1> What I need it look like </h1><br>

<table>
 <thead>
    <tr>
      <th>Month</th>
      <th>Month Total</th>
      <th>Written</th>
      <th>Verbal</th>
      <th>Probable 75%</th>
      <th>Probable 25%</th>
      <th>Speculative</th>
      <th colspan="3"></th>
    </tr>
  </thead>
  <tbody>
  <tr><td>Jan 2017</td> <td>1250</td><td>700</td><td>50</td><td>500</td><td>0</td><td>5000</td></tr>
 <tr> <td>Feb 2017</td> <td>5100</td><td>5000</td><td>100</td><td>0</td><td>0</td><td>0</td></tr>
  <tr><td>Mar 2017</td>  <td>700</td><td>0</td><td>700</td><td>0</td><td>0</td><td>0</td></tr>
    </tbody>
  </table>
  

I'm a bit of a coding noob so I could be going about this all the wrong way, but I've muddled my way through so far...until now.

What I need is to subtotal the months so the values all appear on the relevant line: Here

The code I used for that is:`

      <% @months.each do |t| %>

 <tr>
      <td><%= t.monthYear %> </td> <td><%= t.monthValue %></td> 
      <% if(t.destination.status == "Written") %>
      <td><%= t.monthValue %></td>

      <% else %>
      <td>0</td>
      <% end %>
      <% if(t.destination.status == "Verbal") %>
      <td><%= t.monthValue %></td>
      <% else %>
      <td>0</td>
      <% end %>
      <% if(t.destination.status == "Probable 75%") %>
      <td><%= t.monthValue %></td>
      <% else %>
      <td>0</td>
      <% end %>
      <% if(t.destination.status == "Probable 25%") %>
      <td><%= t.monthValue %></td>
      <% else %>
      <td>0</td>
      <% end %>
      <% if(t.destination.status == "Speculative") %>
      <td><%= t.monthValue %></td>
      <% else %>
      <td>0</td>
      <% end %> 




  </tr>

<% end %>`

The models:

Destination:

class Destination < ActiveRecord::Base
belongs_to :tag
has_many :months

end

Month:

class Month < ActiveRecord::Base
    belongs_to :destination
    belongs_to :tag
end

The months have 3 fields: monthYear, monthValue & Destination_id.
Destination has Status and others which I don't think are relevant.

I've been searching and used:
% @months.select(:monthYear, :monthValue, :destination_id).group(:monthYear).each do |t| %>
and different variations including trying to sum by :monthValue but end up with this: Subtotaled months but not values.

Thanks in advance!

  • Welcome to stackoverflow. Please include the pictures in the question itself, not as links. Better yet to write it as a table. It is also slightly unclear what part is working and what is not. – Eyeslandic May 09 '17 at 17:19
  • 1
    Hey Iceman, thanks for the response. I don't have enough rep points yet to post pictures in the question. I've updated it now with a table of what it looks like and what I need it to look like! I need to subtotal all of the months that have the same name so it appears Jan, Feb, Mar and all of the totals that relate to them all on the same line and totaled. – Jack Stovell May 09 '17 at 17:43
  • If only I could understand the relationship between your entities… – Igbanam May 15 '17 at 15:00

1 Answers1

0

So I figured it out, but the answer definitely isn't pretty! I appreciate this could be done a lot clearer, but I've muddled my way through! Here's the code I used and the result:

View

  <tbody>
<% @months.group(:monthYear).each do |z| %>

      <% this_thing = 0 %>
      <% this_thing2 = 0 %>
      <% this_thing3 = 0 %>
      <% this_thing4 = 0 %>
      <% this_thing5 = 0 %>

     <% @testing.each do |b| %>  

         <% if (z.monthYear == b.monthYear) && (b.destination.status == "Written") %>
           <%  this_thing =  this_thing + b.monthValue %>
         <% end %>


         <% if (z.monthYear == b.monthYear) && (b.destination.status == "Verbal") %>
             <%  this_thing2 =  this_thing2 + b.monthValue %>
         <% end %>


         <% if (z.monthYear == b.monthYear) && (b.destination.status == "Probable 75%") %>
             <%  this_thing3 =  this_thing3 + b.monthValue %>
         <% end %>


         <% if (z.monthYear == b.monthYear) && (b.destination.status == "Probable 25%") %>
              <%  this_thing4 =  this_thing4 + b.monthValue %>
         <% end %>

         <% if (z.monthYear == b.monthYear) && (b.destination.status == "Speculative") %>
            <%  this_thing5 =  this_thing5 + b.monthValue %>
         <% end %>

       <% end %>
       <td><%=z.monthYear %></td>
       <td><%= this_thing + this_thing2 + this_thing3 + this_thing4 + this_thing5 %> </td>
       <td><%= this_thing %></td>
       <td><%=  this_thing2 %></td>
       <td><%=  this_thing3 %></td>
       <td><%=  this_thing4 %></td>
       <td><%=  this_thing5 %></td>
       <td><%= (this_thing * 1)+(this_thing2 * 1)+(this_thing3 *0.75)+(this_thing4 * 0.25)+(this_thing5 * 0)%></td>
</tr>
<% end %>
 </tbody>

Tags Controller

def index
@months = Month.all
@testing = Month.joins(:destination).includes(:destination).select("months.monthYear, months.monthValue, destination_id, destinations.status")

end

The end result!

Sexy AF