我发货有一张发票;发票属于发货。货运表是包含customer_id的货运单。
我需要找到所有发票......
我尝试了很多不同的方法,但似乎都没有用,最后一个让我错误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}
答案 0 :(得分:1)
这有用吗?
@shipments = Shipment.joins(:invoice).where(:customer_id => @customer.id).where("customer_account_balance <> 0")
听起来你的架构看起来像这样:
Shipment: (customer_id, ...)
Invoice: (customer_open_balance, shipment_id, ...)
您是否将has_one :invoice
放入Shipment.rb并将belongs_to :shipment
放入Invoice.rb?
答案 1 :(得分:1)
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