title: Rails 5.1 different sum calculation with multiple currencies
body: <p>I'm working on a Rails app for my expenses.<br>
  Basically I enter some expenses giving a name, description, amount, currency and a date.<br>
  For this I have 3 models : User, Spending, Currency with this relations :  </p>
  
  <pre><code>class Currency &lt; ApplicationRecord
  
      has_many :spendings
      has_many :users, through: :spendings
  
  end
  
  class Spending &lt; ApplicationRecord
  
      belongs_to :user
      belongs_to :currency
  
  end
  
  class User &lt; ApplicationRecord
  
    has_many :spendings
    has_many :currencies, through: :spendings
  
  end
  </code></pre>
  
  <p>I'm using the show from the users_controller to index each user expenses. This is what I have in my users_controller</p>
  
  <pre><code>class UsersController &lt; ApplicationController
  
      def show
          @user_spendings = @current_user.spendings.all.order('date DESC').paginate(:page =&gt; params[:page], :per_page =&gt; 10)
          @user_amount = @user_spendings.sum(:amount)
      end
  end
  </code></pre>
  
  <p>In this scenario the @user_amount shows me all the expenses from the current_user, the thing is that I have 2 currencies (maybe more in the future) and I would like to show different total amount depending on which currency has been selected when I create a new expense.<br>
  I thought about different things here, I tried to do a If statement so that the amount shows only if the currency_id == to 1 if € or to 2 if $ etc... But this wont work well if I add new currencies (and I couldn't make it work).<br>
  Maybe a loop ? Loop through the currencies and somehow show the total amount. But you can't do a sum in a loop so I don't know.<br>
  Also I'd like it to be flexible, so if in the future I add more currencies that I don't have to touch the code.<br>
  Any idea ?<br>
  If needed here is my show.html.erb  </p>
  
  <pre><code>  &lt;% @user_spendings.each do |spending| %&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;%= spending.title %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= spending.description %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= '%.02f' % spending.amount %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= spending.currency.symb %&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%= spending.date.strftime('%d %B %Y') %&gt;&lt;/td&gt;
    &lt;/tr&gt;
    &lt;% end %&gt;
  </code></pre>
  
  <p>Thanks alot.</p>