Reputation: 10224
I have shipment has one invoice; invoice belongs to shipment. The shipments table is the one that contains the customer_id.
I need to find all invoices...
I have tried many different approaches but none seem to work, this last one got me error private method select
or something like that...
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}
Upvotes: 0
Views: 1516
Reputation: 6633
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
Then try
#for a particular customer with id 1
Invoice.with_customer 1
#that have customer_account_balance of 0
Invoice.cero_balance
Upvotes: 1
Reputation: 1425
Does this work?
@shipments = Shipment.joins(:invoice).where(:customer_id => @customer.id).where("customer_account_balance <> 0")
It sounds like your schema looks like this:
Shipment: (customer_id, ...)
Invoice: (customer_open_balance, shipment_id, ...)
Did you put has_one :invoice
in Shipment.rb and belongs_to :shipment
in Invoice.rb?
Upvotes: 1