hmmmnnn. for one, you shouldn't be using an instance variable as it does 
not need to live outside the scope of the function. that being said, 
there is a better way of handling this anyhow.

Make sure that the tables are normalized, it is the best way to allow 
rails to figure out all the associations without your help.

I am assuming that the code above is from an activerecord class called 
"Customer" where each customer may have many "transactions"?

If this is the case, make sure that your "transactions" table has a 
field called "customer_id".

that way you can do this now:

class Customer < ActiveRecord::Base
    has_many :transactions

    def running_total
        #http://www.ruby-doc.org/core/classes/Enumerable.html#M003160
        transactions.inject{|m,v| m.amount.to_f + v.amount.to_f}
    end
end

class Transaction < ActiveRecord::Base
    belongs_to :customer
end

check this link out on associations:
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

You can check out what's going on here by using 'script/console' from 
within your rails working environment:

a...@aldo-desktop:~/Documents/ruby/stuffses$ script/console
Loading development environment (Rails 2.1.0)
>> customer = Customer.find_by_id(1)
>> customer.transactions #will list all associated transactions



HTH,

Aldo Sarmiento



-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to