我有一個在Rails 4項目中設置像這樣的幾個模型。有條件似乎去評估
student.rb
class Student < ActiveRecord::Base
has_many :contracts
def balance
unpaid_contracts.sum(&:sum_total)
end
private
def unpaid_contracts
unpaid_contracts = []
contracts.each do |c|
unpaid_contracts << c if c.unpaid? # This is the problem
end
end
end
contract.rb
class Contract < ActiveRecord::Base
has_many :invoices, dependent: :destroy
belongs_to :student
def unpaid?
return true if invoices.blank?
invoices.each do |i|
return true if i.current_status != "paid" && i.current_status != "rescinded"
end
end
end
無論我做什麼所有的合同被添加到unpaid_contracts
。在db中根本沒有發票,所以我會假設unpaid?
應該總是評估爲真,對不對?
事實上,即使我將條件更改爲unpaid_contracts << c if false
問題仍然存在。
您是否嘗試過加載合同並使用'.unpaid?'它返回什麼? – meagar