[英]Rails PG::UndefinedTable: ERROR: missing FROM-clause entry for table
[英]Ruby on Rails, missing FROM-clause entry for table “pages”
在将Rails应用程序从3.0转换为4.29之后尝试解决这个讨厌的错误
我有一个附加到模型PostType的Scope,它抱怨它需要访问模型Pages,这里是原始代码:
class PostType < ActiveRecord::Base
scope :by_domain, lambda {
|domain| includes([:pages]).where("pages.domain LIKE ?", domain)
}
它抛出了这个错误信息:
PostType Load (1.2ms) SELECT "post_types".* FROM "post_types" WHERE (pages.domain LIKE 'test.com')
PG::UndefinedTable: ERROR: missing FROM-clause entry for table "pages"
LINE 1: SELECT "post_types".* FROM "post_types" WHERE (pages.domain ...
^
: SELECT "post_types".* FROM "post_types" WHERE (pages.domain LIKE 'test.com')
现在我可以用老式的SQL来解决它:
SELECT "post_types".* FROM "post_types", "pages" WHERE (pages.domain LIKE 'test.com')
哪个有效,但我不知道如何将其转换为Rails,有点生疏。
有任何想法吗?
查询post_types
的方式需要显式引用pages
表。 省略references
不会JOIN
pages
表,从而导致错误。
scope :by_domain, lambda {
|domain| includes([:pages]).where("pages.domain LIKE ?", domain).references(:pages)
}
这基本上告诉Rails ORM查询字符串引用了pages
表,因此它将添加一个连接。
看一下API文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.