How to find related records in Ruby on Rails? -
i have class:
class invoice < activerecord::base has_many :payments def payable? amount_payable != 0 end end
how can total
of payments
invoice
payable
?
class payment < activerecord::base belongs_to :invoice def self.total where("invoices.payable? = ?", true).map(&:amount).sum end end
the problem seems where
clause accepts database column names , not functions payable?
.
can tell me how this?
thanks help.
you cannot run ruby methods in sql. logic simple in one, can do
where("amount_payable != ?", 0)
if ruby method cannot expressed in pure sql, way fetch records, filter them in ruby (which inefficient), this:
all.select(&:payable?)
Comments
Post a Comment