本文介绍了PG::UndefinedTable:错误:在使用连接和 where 时缺少表的 FROM 子句条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型,CourierOrder.

I have two models, Courier and Order.

我有以下查询:

active_couriers = Courier.
  available_courier_status.
  where(:service_region_id => @service_region.id).
  includes(:orders)

此查询有效,但它会提取所有订单.我想将订单限制为当天的订单.所以我添加了以下查询 where("orders.created_at >= ?", Time.zone.now.beginning_of_day).

This query works, however, it pulls in all orders. I want to limit the orders to only orders for the day. So I added the following query where("orders.created_at >= ?", Time.zone.now.beginning_of_day).

active_couriers = Courier.
  available_courier_status.
  where(:service_region_id => @service_region.id).
  includes(:current_orders).
  includes(:orders).
  where("orders.created_at >= ?", Time.zone.now.beginning_of_day)

这给了我错误:

PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "orders"

我在这里做错了什么?

推荐答案

嗯,看起来您正在尝试包含 current_ordersorder.这些是不同条件的相同表吗?这可能会混淆活动记录.另外,我很确定在引用连接表时包含 references 方法是明智的.也许,尝试这样的事情:

Hmm it looks like you're trying to include current_orders and include order. Are these the same tables with different conditions? This might be confuse active record. Also, I'm pretty sure it's wise to include the references method when referencing a joined table. Perhaps, try something like this:

active_couriers = Courier.includes(:orders)
  .available_courier_status
  .where(:service_region_id => @service_region.id)
  .where("orders.created_at >= ?", Time.zone.now.beginning_of_day)
  .references(:orders)

这篇关于PG::UndefinedTable:错误:在使用连接和 where 时缺少表的 FROM 子句条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 10:05