2017-04-21 1 views
0

의 합계를 얻기 : 청구서 및 지불액티브 나는이 개 모델이 모든 관련 기록

나는 송장의 관점에서 요약 할 필요가
# == Schema Information 
# 
# Table name: invoices 
# 
# id   :integer   not null, primary key 
# totale  :decimal(8, 3) 
# .... 
# .... 
# 

class Invoice < ActiveRecord::Base 
    has_many :payments 

end 




# == Schema Information 
# 
# Table name: payments 
# 
# id   :integer   not null, primary key 
# invoice_id :integer 
# importo :decimal(8, 3) 
# .... 
# 

class Payment < ActiveRecord::Base 
    belongs_to :invoice 

end 

을 각 송장 반복하지 않고 모든 지불의 importo의 합계.

어떻게 찾을 수 있습니까? 나는 시도했다

@fatture = Invoice.all 
@fatture.payment.sum(:importo) 

succes !!

답변

0

제가 알기로, 당신은 루비 지불에 대한 반복없이 모든 청구서와 관련된 모든 지불의 '수입'을 합산하려고합니다.

나는 당신이 필요로하는 것이 청구서에 지불의 결합이라고 믿습니다.

Invoice.joins(:payments).sum('payments.importo') 

Rails Docs

+0

좋아! 내 문제를 해결해 주셔서 감사합니다! – EffeBi