Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6044305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:59:47+00:00 2026-05-23T06:59:47+00:00

Live site: http://iatidata.heroku.com Github: https://github.com/markbrough/IATI-Data Based on aid information released through the IATI Registry:

  • 0
  • Live site: http://iatidata.heroku.com
  • Github: https://github.com/markbrough/IATI-Data
  • Based on aid information released through the IATI Registry: iatiregistry.org

I’m a bit of a Rails n00b so sorry if this is a really stupid question.

There are two key Models in this app:

  • Activity – which contains details
    such as recipient country, funding
    organisation
  • Transaction – which contains details such as how much money (value) was committed or disbursed (transaction_type), when, to whom, etc.

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.

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

@activities.each do |activity|
    activity.transactions.each do |transaction|
        transaction.value # do something like display it
    end
end

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

What’s the best way to do this? I guess I could do something like:

@totalvalue = 0
@activities.each do |activity|
    activity.transactions.each do |transaction|
        @totalvalue = @totalvalue + transaction.value
    end 
end

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

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 🙂

Thanks very much for any help!

Update:

Thanks for all the responses! So, this works now:

@thiscountry_activities.each do |a|
    @thiscountry_value = @thiscountry_value + a.transactions.sum(:value)
end

But this doesn’t work:

@thiscountry_value = @thiscountry_activities.transactions.sum(:value)

It gives this error:

undefined method `transactions' for #<Array:0xb5670038>

Looks like I have some sort of association problem. This is how the models are set up:

class Transaction < ActiveRecord::Base
    belongs_to :activity
end

class Activity < ActiveRecord::Base
    has_and_belongs_to_many :policy_markers
    has_and_belongs_to_many :sectors
    has_many :transactions
end

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

Thanks again!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:59:48+00:00Added an answer on May 23, 2026 at 6:59 am

    Use Active Record’s awesome sum method, available for classes:

    Transaction.sum(:value)
    

    Or, like you want, associations:

    activity.transactions.sum(:value)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Every time I click on Discussion and Live Support on my site http://riskeraser.com/ firefox
Just adding my site - http://ahardenjr.com - to my live host and noticed my
On http://www.slideshare.net/eljefe/automated-deployment-with-phing in slide 15 they are talking about locking the live site when
I found this site: http://www.silobreaker.com/FlashNetwork.aspx?DrillDownItems=11_81046 and i'm trying to figure out how such a
I have a backup server that automatically backs up my live site, both files
When you roll out changes to a live web site, how do you go
We deployed a live, fresh, swanky site using preview 3 including rigorous stress testing.
Microsoft's new Windows Live Application Based Storage API is a RESTful API. More info
I have a live site that includes different php files depending on what page
I have the following code, which works fine on live site, but not on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.