2017-06-19 72 views
0

這是一個非常簡單的關係,我試圖連接並運行一個where子句。這在Rails 3中運行良好,但不再適用於Rails 4.語法對我來說都是正確的。所有記錄關係如listing.sellerseller.listings均按預期工作。PG :: UndefinedTable錯誤與Rails 4中的關聯where子句

class Listing < ActiveRecord::Base 
    belongs_to :seller, class_name: "User" 
end 

然而

Listing.joins(:seller).where({ seller: { id: 1 } }) 
# Or 
Listing.eager_load(:seller).where({ seller: { id: 1 } }) 

# Both result in the following error: 
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: missing FROM-clause entry for table "seller" 
LINE 1: ...s" ON "users"."id" = "listings"."seller_id" WHERE "seller"."... 

回答

1

在你必須使用實際的表名where子句。既然你沒有遵循慣例,這個問題就出現了。

Listing.joins(:seller).where({ users: { id: 1 } }) 
# Or 
Listing.eager_load(:seller).where({ users: { id: 1 } }) 
+0

謝謝!那就是訣竅。 –

相關問題