繁体   English   中英

Ruby on Rails,缺少表“pages”的FROM子句条目

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM