denied39 wrote:
> Ok, so I'm really new to Ruby and Rails. I'm trying to create a home
> budget app, and I'm having some difficulties getting a running total
> working. I have the following code in my model:
> def running_total
> @total = 0.0
> @transactions = Transaction.find(:all, :conditions =>
> ["category_id=?", category.id])
> @transactions.each do |transaction|
> @total += transaction.amount.to_f
> return @total
> end
> end
>
> What happens is I get the first expense amount, but it repeats for
> every row in the table.
> Date Payee Amount Total
> 2008-12-03 test 17.98 17.98
> 2008-12-03 test 0.57 17.98
> 2008-12-03 test 19.04 17.98
>
> This is my first app with Rails, so I'm not sure everything is in the
> right place. I created a Ruby script that contains the following:
> result = dbh.query("select amount from transactions where category_id
> = 6")
> test = 0.0
> result.each do |row|
> test += row[0].to_f
> puts test
> end
>
> The script works like it should, it increments the total after every
> pass. Not sure what I'm doing wrong with the Rails portion. Any help
> would be great. If you need me to post any more detail, just let me
> know.
>
> >
>
>
Your loop ends with the "return @total" instead of totaling the
transactions. Move the return down to after the end and I think you
will be happier.
Norm
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---