ActiveRecord :: StatementInvalid:PG :: UndefinedTable:错误:在rails查询中

时间:2016-02-18 12:41:46

标签: ruby-on-rails include spree

您好我已经包含了代码

 Spree::Order.includes(:ship_address).where('ship_address.country_id = ?', 52).references(:spree_address).count

我面临错误

 (1.1ms)  SELECT COUNT(DISTINCT "spree_orders"."id") FROM "spree_orders" LEFT OUTER JOIN "spree_addresses" ON "spree_addresses"."id" = "spree_orders"."ship_address_id" WHERE (ship_address.country_id = 52)
PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "ship_address"
LINE 1: ...s"."id" = "spree_orders"."ship_address_id" WHERE (ship_addre...
                                                             ^
: SELECT COUNT(DISTINCT "spree_orders"."id") FROM "spree_orders" LEFT OUTER JOIN "spree_addresses" ON "spree_addresses"."id" = "spree_orders"."ship_address_id" WHERE (ship_address.country_id = 52)
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "ship_address"
LINE 1: ...s"."id" = "spree_orders"."ship_address_id" WHERE (ship_addre...
                                                             ^
: SELECT COUNT(DISTINCT "spree_orders"."id") FROM "spree_orders" LEFT OUTER JOIN "spree_addresses" ON "spree_addresses"."id" = "spree_orders"."ship_address_id" WHERE (ship_address.country_id = 52)
from /Users/TopFormInvestment/.rvm/gems/ruby-2.1.4@skinnymint/gems/rack-mini-profiler-0.9.2/lib/patches/sql_patches.rb:160:in `exec'

疯狂加入model

belongs_to :ship_address, foreign_key: :ship_address_id, class_name: 'Spree::Address'
    alias_attribute :shipping_address, :ship_address

请指导我如何解决此错误。

1 个答案:

答案 0 :(得分:1)

查看以下查询是否有帮助。

Spree::Order.includes(:ship_address).where('spree_addresses.country_id = ?', 52).references(:spree_address).count