D
ddoherty03
All,
I have a rails app with a Transaction class that has_many :entries. It
starts like this. Each Transaction has many edits, and those with the
debit attribute true are debits, false are credits.
class Transaction < ActiveRecord::Base
has_many :entries
has_many :debits, :class_name => 'Entry', :conditions => {:debit => true}
has_many :credits, :class_name => 'Entry', :conditions => {:debit => false}
end
I want to be able to complete an Transaction if there is exactly one
Entry that has its amount set to nil. I want to set that amount to a
debit or credit of the proper amount to make the Transaction balance.
Sounded easy to me, but I am having trouble with my balance! method.
The relevant part of it is:
def balance!
...
if num_nils == 1
# Set the sole nil entry to amount needed to balance Transaction
plug = debit_total - credit_total
entries.whereamount => nil).each do |e|
e.amount = plug.abs
e.debit = (plug < 0)
e.save
end
end
The problem is that when I finish this, the entries associated with my
Transaction are not affected. I appears to make a copy, change it, and
leave the Entry associated with my Transaction with a nil amount.
What am I missing?
Thanks,
Dan Doherty
I have a rails app with a Transaction class that has_many :entries. It
starts like this. Each Transaction has many edits, and those with the
debit attribute true are debits, false are credits.
class Transaction < ActiveRecord::Base
has_many :entries
has_many :debits, :class_name => 'Entry', :conditions => {:debit => true}
has_many :credits, :class_name => 'Entry', :conditions => {:debit => false}
end
I want to be able to complete an Transaction if there is exactly one
Entry that has its amount set to nil. I want to set that amount to a
debit or credit of the proper amount to make the Transaction balance.
Sounded easy to me, but I am having trouble with my balance! method.
The relevant part of it is:
def balance!
...
if num_nils == 1
# Set the sole nil entry to amount needed to balance Transaction
plug = debit_total - credit_total
entries.whereamount => nil).each do |e|
e.amount = plug.abs
e.debit = (plug < 0)
e.save
end
end
The problem is that when I finish this, the entries associated with my
Transaction are not affected. I appears to make a copy, change it, and
leave the Entry associated with my Transaction with a nil amount.
What am I missing?
Thanks,
Dan Doherty