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

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -