Rails - subtraction based on if statements

<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>
  
      

Run codeHide result


I'm coding a little noob, so I could go this route, but I'm still confusing my way ... so far.

I need the intermediate months so that the values โ€‹โ€‹appear on the appropriate line: Here

The code I used to do this: `

      <% @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%> `

Models:

Purpose:

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

      

end

Month

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

      

Months have 3 fields: monthYear, monthValue and Destination_id.
The destination has status and others that I believe are not relevant.

I have searched and used:
% @months.select(:monthYear, :monthValue, :destination_id).group(:monthYear).each do |t| %>

and various options, including trying to sum: monthValue, but in the end: Intermediate months but not values.

Thanks in advance!

+3


source to share


1 answer


So I got it, but the answer is definitely not very good! I appreciate it can be made much clearer, but I am confused! 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>

      

Tag controller



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

end

      

Final result!

Sexy AF

0


source







All Articles