title: Rails: Sum of values in all Transactions that belong_to an Activity
body: <ul>
  <li>Live site: <a href="http://iatidata.heroku.com" rel="nofollow">http://iatidata.heroku.com</a>   </li>
  <li>Github: <a href="https://github.com/markbrough/IATI-Data" rel="nofollow">https://github.com/markbrough/IATI-Data</a></li>
  <li>Based on aid information released through the IATI Registry: iatiregistry.org</li>
  </ul>
  
  <p>I'm a bit of a Rails n00b so sorry if this is a really stupid question.</p>
  
  <p>There are two key Models in this app:</p>
  
  <ul>
  <li>Activity - which contains details
  such as recipient country, funding
  organisation</li>
  <li>Transaction - which contains details such as how much money (value) was committed or disbursed (transaction_type), when, to whom, etc.</li>
  </ul>
  
  <p>All Transactions nest under an Activity. Each Activity has multiple Transactions. They are connected together by activity_id. has_many :transactions and belongs_to :activity are defined in the Activity and Transaction Models respectively.</p>
  
  <p>So: all of this works great when I'm trying to get details of transactions for a single activity - either when looking at a single activity (activity->show) or looping through activities on the all activities page (activity->index). I just call </p>
  
  <pre><code>@activities.each do |activity|
      activity.transactions.each do |transaction|
          transaction.value # do something like display it
      end
  end
  </code></pre>
  
  <p>But what I now really want to do is to get the sum of all transactions for all activities (subject to :conditions for the activity).</p>
  
  <p>What's the best way to do this? I guess I could do something like:</p>
  
  <pre><code>@totalvalue = 0
  @activities.each do |activity|
      activity.transactions.each do |transaction|
          @totalvalue = @totalvalue + transaction.value
      end 
  end
  </code></pre>
  
  <p>... but that doesn't seem very clean and making the server do unnecessary work. I figure it might be something to do with the model...?! sum() is another option maybe?</p>
  
  <p>This has partly come about because I want to show the total amount going to each country for the nice bubbles on the front page :)</p>
  
  <p>Thanks very much for any help!</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Thanks for all the responses! So, this works now:</p>
  
  <pre><code>@thiscountry_activities.each do |a|
      @thiscountry_value = @thiscountry_value + a.transactions.sum(:value)
  end
  </code></pre>
  
  <p>But this doesn't work:</p>
  
  <pre><code>@thiscountry_value = @thiscountry_activities.transactions.sum(:value)
  </code></pre>
  
  <p>It gives this error:</p>
  
  <pre><code>undefined method `transactions' for #&lt;Array:0xb5670038&gt;
  </code></pre>
  
  <p>Looks like I have some sort of association problem. This is how the models are set up:</p>
  
  <pre><code>class Transaction &lt; ActiveRecord::Base
      belongs_to :activity
  end
  
  class Activity &lt; ActiveRecord::Base
      has_and_belongs_to_many :policy_markers
      has_and_belongs_to_many :sectors
      has_many :transactions
  end
  </code></pre>
  
  <p>I think this is probably quite a simple problem, but I can't work out what's going on. The two models are connected together via id (in Activity) and activity_id (in Transactions).</p>
  
  <p>Thanks again!</p>