Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 轨道3。如何进行嵌套属性查询?_Ruby On Rails_Ruby_Activerecord - Fatal编程技术网

Ruby on rails 轨道3。如何进行嵌套属性查询?

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

我有一份发货发票;发票属于装运货物。装运表是包含客户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_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