Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.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
Rails SQL-将一个表中的数量总和与联接表中的值进行比较_Sql_Ruby On Rails - Fatal编程技术网

Rails SQL-将一个表中的数量总和与联接表中的值进行比较

Rails SQL-将一个表中的数量总和与联接表中的值进行比较,sql,ruby-on-rails,Sql,Ruby On Rails,我试图根据订单总额计算收到的项目数量之和,以确保在出现部分订单的情况下订单已关闭 我希望能够创建范围,我将能够返回所有打开的订单,然后所有关闭的订单 class JobquoteOrder < ActiveRecord::Base belongs_to :jobquote belongs_to :order has_many :po_receipts, dependent: :restrict_with_exception has_many :receipts, throu

我试图根据订单总额计算收到的项目数量之和,以确保在出现部分订单的情况下订单已关闭

我希望能够创建范围,我将能够返回所有打开的订单,然后所有关闭的订单

class JobquoteOrder < ActiveRecord::Base
  belongs_to :jobquote
  belongs_to :order
  has_many :po_receipts, dependent: :restrict_with_exception
  has_many :receipts, through: :po_receipts

  scope :receipts, -> {PoReceipt.uniq.pluck(:jobquote_order_id)}

  scope :summed_receipts, ->(jq_id) {PoReceipt
    .where(jobquote_order_id: jq_id)
    .sum(:qty)}

  scope :open_orders, -> {find(receipts)
    .where("jobquote_orders.qty >= #{summed_receipts(:id)}")}
end

我提出这个方法是为了让它工作,但我仍然想知道是否可以用纯SQL来完成。任何关于重构的建议都将受到欢迎

class JobquoteOrder < ActiveRecord::Base
  belongs_to :jobquote
  belongs_to :order
  has_many :po_receipts, dependent: :restrict_with_exception
  has_many :receipts, through: :po_receipts
  validates_presence_of :jobquote_id, :order_id, :qty, :total_cost
  validates_uniqueness_of :jobquote_id, scope: [:order_id, :qty, :total_cost], message: 'That order already exists.'

  scope :receipts, -> {PoReceipt.uniq.pluck(:jobquote_order_id)}
  scope :summed_receipt, ->(jq_id) {PoReceipt.where(jobquote_order_id: jq_id)
.sum(:qty)}

  scope :open_orders, -> {find(o_c_orders("open"))}

  scope :closed_orders, -> {find(o_c_orders("closed"))}

  def self.o_c_orders(which)
    ret_arr = []
    case which
    when "open"
      receipts.each do |id|
        ret_arr << id unless is_closed?(id)
      end
      ret_arr << self.includes(:po_receipts).where(:po_receipts => {:id => nil}).pluck(:id)
    when "closed"
      receipts.each do |id|
        ret_arr << id if is_closed?(id)
      end
    end
    return ret_arr
  end

  def self.is_closed?(jq_id)
    jq = self.find(jq_id)
    if jq.qty <= summed_receipt(jq_id)
      true
    else
      false
    end
  end
end    
class JobquoteOrder{PoReceipt.uniq.pull(:jobquote\u order\u id)}
范围:汇总收据,->(jq\u id){PoReceipt.where(jobquote\u order\u id:jq\u id)
.sum(:数量)}
范围:未结订单,->{find(未结订单)(“未结”)}
范围:关闭订单,->{find(o_c_订单(“关闭”)}
def self.o_c_订单(其中)
ret_arr=[]
哪种情况
“打开”时
收据。每个do | id|
ret_arr nil}).pull(:id)
“关闭”时
收据。每个do | id|

ret_arr您似乎遗漏了范围定义中的参数
:id
,以
打开订单
。您应该调用
JobquoteOrder.open_orders
id
。我更新了作用域以包含id,但现在我看到我正在尝试对id数组而不是对象数组执行where。这
:id
中的位置是哪里
来自?我试图从find数组检索的记录中提取
id
。我不确定这是不是一种方法。先忘掉
scope
,你能用一个脚本或SQL实现你的逻辑吗?
class JobquoteOrder < ActiveRecord::Base
  belongs_to :jobquote
  belongs_to :order
  has_many :po_receipts, dependent: :restrict_with_exception
  has_many :receipts, through: :po_receipts
  validates_presence_of :jobquote_id, :order_id, :qty, :total_cost
  validates_uniqueness_of :jobquote_id, scope: [:order_id, :qty, :total_cost], message: 'That order already exists.'

  scope :receipts, -> {PoReceipt.uniq.pluck(:jobquote_order_id)}
  scope :summed_receipt, ->(jq_id) {PoReceipt.where(jobquote_order_id: jq_id)
.sum(:qty)}

  scope :open_orders, -> {find(o_c_orders("open"))}

  scope :closed_orders, -> {find(o_c_orders("closed"))}

  def self.o_c_orders(which)
    ret_arr = []
    case which
    when "open"
      receipts.each do |id|
        ret_arr << id unless is_closed?(id)
      end
      ret_arr << self.includes(:po_receipts).where(:po_receipts => {:id => nil}).pluck(:id)
    when "closed"
      receipts.each do |id|
        ret_arr << id if is_closed?(id)
      end
    end
    return ret_arr
  end

  def self.is_closed?(jq_id)
    jq = self.find(jq_id)
    if jq.qty <= summed_receipt(jq_id)
      true
    else
      false
    end
  end
end