Rails summary with sub-totals by date -


i'm writing first rails view summarizes data date. want 1 row each date columns summarized date.

i have been able make work. but, it's awkward coding. have:

<h3>carwings daily summary</h3> <table class="display datatable table table-striped table-bordered"     id="datatable2">   <thead>   <tr>     <th>date</th>     <th># trips</th>     <th>e consumption (kwh)</th>     <th>e regeneration (kwh)</th>     <th>e total (kwh)</th>     <th>distance (miles)</th>     <th>energy economy (miles/kwh)</th>     <th>co2 emission reduction (lbs)</th>   </tr>   </thead>   <tbody>   <% trips = 0 %>   <% consumption = 0 %>   <% regen = 0 %>   <% total = 0 %>   <% distance = 0 %>   <% economy = 0 %>   <% emissions = 0 %>   <% sumdate = nil %>   <% @carwings.each.with_index |carwing, index| %>       <% sumdate = carwing.date if index == 0 %>       <% if carwing.date == sumdate %>           <% trips = trips + 1 %>           <% consumption = consumption + carwing.e_consumption %>           <% regen = regen + carwing.e_regen %>           <% total = total + carwing.e_total %>           <% distance = distance + carwing.distance %>           <% economy = economy + carwing.economy %>           <% emissions = emissions + carwing.emission_reduction %>       <% else %>           <tr>             <td class="nowrap"><%= sumdate %></td>             <td><%= trips %></td>             <td><%= consumption %></td>             <td><%= regen %></td>             <td><%= total %></td>             <td><%= distance %></td>             <td><%= economy %></td>             <td><%= emissions %></td>           </tr>           <% trips = 1 %>           <% consumption = carwing.e_consumption %>           <% regen = carwing.e_regen %>           <% total = carwing.e_total %>           <% distance = carwing.distance %>           <% economy = carwing.economy %>           <% emissions = carwing.emission_reduction %>           <% sumdate = carwing.date %>       <% end %>   <% end %>   <tr>     <td class="nowrap"><%= sumdate %></td>     <td><%= trips %></td>     <td><%= consumption %></td>     <td><%= regen %></td>     <td><%= total %></td>     <td><%= distance %></td>     <td><%= economy %></td>     <td><%= emissions %></td>   </tr>   </tbody> </table> 

there's got better way.

suggestions?

thanks help!!

some minor stuff:

trips = trips + 1 # better written as: trips += 1 

erb tags can multiline eg:

<% if blah      do_something      else    end %> 

if setting multiple variables same value, don't need repeat them each line eg:

trips = consumption = regen = 0 

yes - minor stuff - clean minor stuff , it'll give better shape of you're trying do.

perhaps if gave logic in descriptive pseudocode (so aren't guessing you're trying do) can give better structure code too. :)

personally: i'd recommend setting data in controller (or carwing model) before ever hits view. i'd use hash - whee carwnig.date key, , rest hash eg:

data = hash.new({:trips => 0, :consumption => 0}) # google initialising hashes @carwings.each |carwing|   data[carwing.date][:trips] += 1   data[carwing.date][:consumption] += carwing.e_consumption   # etc end  

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -