Mysql 在rails中使用Where和all进行搜索

Mysql 在rails中使用Where和all进行搜索,mysql,ruby,activerecord,ruby-on-rails-5,Mysql,Ruby,Activerecord,Ruby On Rails 5,我正在从事一个rails项目,该项目有两个模型-传入的\u购买和项目\u实例。传入采购具有多个item_实例,并且item_实例属于传入采购。我的问题是,我想显示所有已经收到的订单,要做到这一点,每个item_实例必须从订单中接收。item_实例模型有一个date_received列。这样我就可以下订单了,我们打电话1 订单1:有两个项目实例,其中一个项目已收到: 项目1:收到日期:20年9月15日 第2项:收到日期:无 我需要显示记录,其中填写了接收日期的任何值,以便接收订单。因此,不会收到上

我正在从事一个rails项目,该项目有两个模型-传入的\u购买和项目\u实例。传入采购具有多个item_实例,并且item_实例属于传入采购。我的问题是,我想显示所有已经收到的订单,要做到这一点,每个item_实例必须从订单中接收。item_实例模型有一个date_received列。这样我就可以下订单了,我们打电话1

订单1:有两个项目实例,其中一个项目已收到: 项目1:收到日期:20年9月15日 第2项:收到日期:无

我需要显示记录,其中填写了接收日期的任何值,以便接收订单。因此,不会收到上述示例。到目前为止,我已经编写了一个如下所示的范围:

scope :search_for_received_orders, ->{ joins(:item_instances).where.not('item_instances.date_received': [nil] ).distinct }
这将使我得到上面的例子,这将是一个部分填充的订单。我的代码用于查看未收到的订单,如下所示:

  scope :search_for_not_received_orders, ->{ joins(:item_instances).where('item_instances.date_received': [nil, ""] ).distinct }
基本上,我需要确保接收订单的所有项目实例在date_received列中都有一个日期。我想不出来。谢谢你的帮助

我收到的第一个响应错误如下:

/home/mcuddy/learn/inventory/src/inventory/app/models/incoming_purchase.rb:23: syntax error, unexpected '}', expecting => ...nces: {!date_received.present?})} ... ^ 

/home/mcuddy/learn/inventory/src/inventory/app/models/incoming_purchase.rb:36: syntax error, unexpected do (for lambda) item_instances.each do |i| ^~ 

/home/mcuddy/learn/inventory/src/inventory/app/models/incoming_purchase.rb:40: syntax error, unexpected end, expecting '}' end ^~~

 scope :received_orders, -> { includes(:item_instances).where.not(item_instances: {!date_received.present?})}
请尝试以下内容,并参考如何使用where条件

scope :search_for_received_orders, 
   ->{ joins(:item_instances)
       .where.not('item_instances.date_received': nil).distinct }


scope :search_for_not_received_orders, 
    ->{ joins(:item_instances)
        .where("item_instances.date_received IS NULL OR item_instances.date_received = ''"}).distinct }
试试这个:

scope :received_orders, -> { includes(:item_instances).where.not(item_instances: {date_received: nil})}

scope :not_received_orders, -> { includes(:item_instances).where(item_instances: {date_received: nil})}
收到的订单没有任何日期为_receivednil的项目实例。
“未收到订单”至少有一个日期为“未收到订单”的项目实例

谢谢Salil-但是搜索“未收到订单”的方法很有效-我将其更改为更好的方法。搜索未完全工作的\u已接收\u订单-获取的订单包含尚未接收的项目实例。谢谢您,catmal-但是,您确定已接收的\u订单是正确的吗?我知道括号在末尾的左边,但当我使用它时仍然会出错——我仍然在使用它。再次感谢你。请看我上面的编辑-在主区域中,再次感谢你提供的任何帮助-这让我一整天都很痛苦!卡特马尔,非常感谢你!它的作品=>真的很讨厌我是多么接近!再次感谢您,祝您周末愉快!