<!-- Step 1: List all transactions, starting with the ICO -->
<ol>
<% for transaction in Transaction.all %>
<% from_user = User.find_by({ "id" => transaction["from_user_id"] }) %>
<% to_user = User.find_by({ "id" => transaction["to_user_id"] }) %>
<li>
<% if from_user %>
<%= transaction["amount"] %> from <%= from_user["name"] %> to <%= to_user["name"] %>
<% else %>
Initial coin offering: <%= transaction["amount"] %> to <%= to_user["name"] %>
<% end %>
</li>
<% end %>
</ol>
<!-- Step 2: Calculate and display the balances -->
<ul>
<% for user in User.all %>
<%
balance = 0
balance = balance + Transaction.where({ "to_user_id" => user["id"] }).sum("amount")
balance = balance - Transaction.where({ "from_user_id" => user["id"] }).sum("amount")
%>
<li>Balance for <%= user["name"] %>: <%= balance %> KC</li>
<% end %>
</ul>
Alternatively, using a <table>
:
<!-- Step 1: List all transactions, starting with the ICO -->
<table>
<tr>
<th>From User</th>
<th>To User</th>
<th>Amount</th>
</tr>
<% for transaction in Transaction.all %>
<% from_user = User.find_by({ "id" => transaction["from_user_id"] }) %>
<% to_user = User.find_by({ "id" => transaction["to_user_id"] }) %>
<tr>
<td>
<% if from_user %>
<%= from_user["name"] %>
<% else %>
Initial coin offering
<% end %>
</td>
<td><%= to_user["name"] %></td>
<td><%= transaction["amount"] %></td>
</tr>
<% end %>
</ol>
<!-- Step 2: Calculate and display the balances -->
<table>
<tr>
<th>User</th>
<th>Balance</th>
</tr>
<% for user in User.all %>
<%
balance = 0
balance = balance + Transaction.where({ "to_user_id" => user["id"] }).sum("amount")
balance = balance - Transaction.where({ "from_user_id" => user["id"] }).sum("amount")
%>
<tr>
<td><%= user["name"] %></td>
<td><%= balance %> KC</td>
</tr>
<% end %>
</table>