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

  • SEARCH
  • Home
  • 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 6039497
In Process

The Archive Base Latest Questions

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

I have a Rails 2.3.11 app which has two key Models: Activity Transaction Live

  • 0

I have a Rails 2.3.11 app which has two key Models:

  • Activity
    • Transaction

Live site: http://iatidata.heroku.com
Github: https://github.com/markbrough/IATI-Data

Every transaction nests under an activity. Each activity has multiple transactions.

I think I’ve got confused about how associations work in Rails, but maybe what I’m trying to do isn’t possible.

Essentially, I want to get the total value of the transactions of all the activities which belong to each country. So how much money went to India, how much to Afghanistan, etc.

This works:

@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>

@thiscountry_activities is defined like this:

@activities = Activity.find(:all, :conditions=> @conditions)

This is placed within a loop which gets each recipient country code. @conditions are :

@conditions[:recipient_country_code]=*each recipient country code, e.g. AF*

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

Thank you!

  • 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:22:38+00:00Added an answer on May 23, 2026 at 6:22 am

    Nothing is broken, the find method in Rails2 returns an Array – and there is no transactions method on the Array class. Use the first thing you have that’s working, with the each iterator, or work from the other side – from Transaction and grouping by the activity. It’s been a while since I wrote Rails2 code, but it’ll be something like this:

    Transaction.all( :select => "activity_id, SUM(value) AS total", :group => 'activity_id' )
    

    That’s not ready to copy-paste into your app as-is because there are some things about your app which aren’t clear to me, but hopefully it sets you in the right direction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Rails app in which I have two models: a Machine model
So In my rails app I have two resources (rentals, and reservations) which belong
I have a sequence of migrations in a rails app which includes the following
In my Rails app, I have a form which redirects through a foreign service,
I have a rails app that has picked up a bit of traction, but
I have a Rails app with some basic models. The website displays data retrieved
In my Rails app I have a fairly standard has_many relationship between two entities.
I have a RoR app running rails 2.3.8 which makes use of the Rails
We're hosting our Ruby on Rails application with the EngineYard App Cloud, which has
I'm trying to work on this Rails app which has the following objectives: /foods/

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.