PG::UndefinedTable: ERROR: missing FROM-clause entry for table when using joins and where

心已入冬 提交于 2019-11-27 18:28:54
Dane O'Connor

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)

You can also use eager_load to provide the same exact behavior as includes + references does. It performs the same Left Outer Join on the table passed as an argument, but in a much cleaner manner.

Docs here: http://apidock.com/rails/v4.2.7/ActiveRecord/QueryMethods/eager_load

Per this example:

active_couriers = Courier.eager_load(:orders)
  .available_courier_status
  .where(:service_region_id => @service_region.id)
  .where("orders.created_at >= ?", Time.zone.now.beginning_of_day)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!