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