2012-04-18 29 views
1

好加載因此,我有以下型號我怎樣做預先加載與包括但限幅最新使用的範圍,包括參數

Class Company < ActiveRecord::Base 
has_many customers 
active? 

Class Customers < ActiveRecord::Base 
belongs_to :Company 
has_one :account 

Class Account < ActiveRecord::Base 
belongs_to :Customer 
has_many :invoices 

Class Invoice < ActiveRecord::Base 
belongs_to :account 
has_many :line_items 
invoice_date 

Class LineItem < ActiveRecord::Base 
belongs_to:Invoice 

我需要做的就是找回什麼: 誰擁有客戶所有活動的公司有的發票爲這些客戶

這裏是擦: 由於每個客戶都能擁有我只想回報發票數額巨大讓說的發票4月1日2012年

什麼I W ant要做的就是使用includes與在模型上設置的範圍或方法相結合來限制返回的發票金額。

我的模型將包含範圍:

Class Invoice < ActiveRecord::Base 
belongs_to :account 
scope :for_the_day_of, lambda{|invoice_date|where('invoices.invoice_date',invoice_date } 

所以,現在我可以寫這樣的事:

invoice = Invoice.for_the_day_of(Date.today).first 

我真的想用它做的是一樣的東西:

comapanies = Company.where(:active => true) 
.includes(:merchants => [:billing_account => 
[:invoices => for_the_day_of(Date.today) => :invoice_line_items]]) 

我不知道這是可能的積極記錄或者我只是想着問題不對

任何幫助將不勝感激。

感謝

回答

1

如果我理解正確的,你需要的是你從另一個模型和一些子查詢功能的訪問範圍。 我爲此建議使用gem Squeel。這是一個偉大的寶石,它擴展了活動記錄功能。

+0

是啊,看起來像我在尋找感謝 – Anthony 2012-04-23 01:19:40