Ruby on rails 轨道3。如何进行嵌套属性查询?
我有一份发货发票;发票属于装运货物。装运表是包含客户id的表 我需要找到所有发票Ruby on rails 轨道3。如何进行嵌套属性查询?,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,我有一份发货发票;发票属于装运货物。装运表是包含客户id的表 我需要找到所有发票 针对特定客户和 客户帐户余额为0的 我尝试过许多不同的方法,但似乎都不管用,最后一种方法让我出错private method select或类似的东西 reports_controller.rb i = Invoice.where("customer_open_balance != 0") s = Shipment.find_by_customer_id(@customer.id) shipment_ids_f
- 针对特定客户和
- 客户帐户余额为0的
private method select
或类似的东西
reports_controller.rb
i = Invoice.where("customer_open_balance != 0")
s = Shipment.find_by_customer_id(@customer.id)
shipment_ids_from_invoices = i.map{|x| x.shipment_id}
@shipments = s.select{|z| shipment_ids_from_invoices.include? z.id}
这行吗
@shipments = Shipment.joins(:invoice).where(:customer_id => @customer.id).where("customer_account_balance <> 0")
您是否将
has_one:invoice
放在了shipping.rb中,并且belts_to:shipping
放在了invoice.rb中?这给了我一些错误,但我喜欢创建范围的方法。我想我会把答案结合起来。谢谢!:)
class Invoice
belongs_to :shipment
scope :with_customer, lambda { |customer_id| joins(:shipment).where(:customer_id => customer_id) }
scope :cero_balance, joins(:shipment).joins(:customer).where("customer_account_balance <> 0")
end
class Invoice
belongs_to :shipment
scope :with_customer, lambda { |customer_id| joins(:shipment).where(:customer_id => customer_id) }
scope :cero_balance, joins(:shipment).joins(:customer).where("customer_account_balance <> 0")
end
#for a particular customer with id 1
Invoice.with_customer 1
#that have customer_account_balance of 0
Invoice.cero_balance