在我Invoice
模型我試圖獲取所有未付發票:如何在函數而不是數據庫列上運行where子句?
class Invoice < ActiveRecord::Base
has_many :payments
def self.outstanding
where("outstanding_amount != 0")
end
private
def outstanding_amount
total - payments.to_a.sum(&:amount_in_cents)
end
end
這不起作用,但是,因爲我沒有一個數據庫列outstanding_amount
。有沒有辦法在函數而不是數據庫字段上運行where
子句?
感謝您的任何幫助。
您可以將搜索條件作爲參數傳遞並讓方法處理它。但在你的情況,如果你只是把它放在: 'def self.outstanding outstanding_amount == 0? ?無:outstanding_amount 結束' –