2014-04-29 34 views
0

在我看來,我調用一個函數Rails的ActiveRecord的總和附條件不工作

def sum_customer_yearly_revenue(customer_id, year) 
    sum_customer_yearly_revenue = Sale.sum(:net_amount, :conditions => ['customer_id = ? AND financial_year = ?', customer_id, year]) 
end 

對於發送到PostgreSQL(如日誌文件查看)某些原因SQL是如下:

SELECT SUM("sales"."net_amount") AS sum_id FROM "sales" 

所有條件正在被忽略。

有沒有人經歷過這個,可以給我一些見解?

回答

7

更新sum_customer_yearly_revenue方法

對於Rails 3.x

def sum_customer_yearly_revenue(customer_id, year) 
    Sale.where(:all, :conditions => ['customer_id = ? AND financial_year = ?', customer_id, year]).sum(:net_amount) 
end 

對於Rails 4.x

def sum_customer_yearly_revenue(customer_id, year) 
    Sale.where(customer_id: customer_id, financial_year: year).sum(:net_amount) 
end 
+0

謝謝你,工作出色!我最初運行Rails 4.0並升級到4.1。是否「.where(:all,:conditions => ['customer_id =?...)等已棄用? –

+1

很高興能幫到:)而且是的,它已被棄用。 –