@Aldo - I tried the inject method, and it looks like it might work,
but I get an undefined method "amount" error. It lists the total of
the first two amount in the error, so it is adding, but somewhere it
is failing in the code. I'm not sure if I have things in the right
model or not, so that could be the issue. The customer model you were
referring to is actually a category model. I tried to move the code
from the transaction model to the category model like you described,
but that also failed to work. Which could very well be me not having
the view looking in the right place.

@Colin - I understand what you mean about the array, but I thought the
@total =+ transactions.amount.to_f would increment the total for each
row. Again, this could be my novice understanding of the web (and
Rails) way of programming. Up to this point, I've only ever written
command line scripts for Perl. I'll definitely give the added array a
try and see if I can make that work.

As I struggle through this, I wonder if I'm just really over-thinking
this problem. Do I really need a running total, or would a final sum
be adequate? I'm going to try the solutions everyone proposed before I
scrap the running total idea.

Thanks for you help so far. I'll post my progress later this evening.

On Jan 23, 4:39 am, Colin Law <[email protected]> wrote:
> The reason the total is the same on every row rather than being a running
> total is that your code calculates the total once for all all transactions
> of the given category. If you want a running total you will have to have an
> array @totals where the first element is the first transaction, the second
> is that plus the second transaction, the third is that plus the third
> transaction and so on, then display this in the view.
> Colin
>
> 2009/1/22 denied39 <[email protected]>
>
>
>
> > Ok, I moved the return @total after the "end", and now I get the
> > following:
>
> > 2008-12-03      test    17.98   191.72
> > 2008-12-03      test    0.57            191.72
> > 2008-12-03      test    19.04   191.72
>
> > (there are more rows, I just cut it to three for the example)
> > So, the total works, but it doesn't increment the total after every
> > row. I seem to get either the first amount, or the sum.
>
> > On Jan 22, 11:14 am, Norm <[email protected]> wrote:
> > > 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to