【发布时间】:2012-04-20 15:23:19
【问题描述】:
好的,所以我有以下型号
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
我需要做的是检索: 拥有客户和这些客户的一些发票的所有活跃公司
问题来了: 由于每个客户都可以拥有大量发票,因此我只想退回 2012 年 4 月 1 日的发票。
我想要做的是使用包括结合模型上设置的范围或方法来限制返回的发票数量。
我的模型将包含范围:
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]])
我不知道这在活动记录中是否可能,或者我只是想问题错误
任何帮助将不胜感激。
谢谢
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 activerecord