Ruby on rails Rails-sql-search-through有一个关系

Ruby on rails Rails-sql-search-through有一个关系,ruby-on-rails,Ruby On Rails,此查询位于notices_controller.rb中: Notice.includes(:active_comment_relationship).where(active_comment_relationship: {id: nil} ).limit(50) 正在生成此错误: PG::UndefinedTable: ERROR: missing FROM-clause entry for table "active_comment_relationship" 我看不出代码有什么问题。我

此查询位于notices_controller.rb中:

Notice.includes(:active_comment_relationship).where(active_comment_relationship: {id: nil} ).limit(50)
正在生成此错误:

PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "active_comment_relationship"
我看不出代码有什么问题。我已经看过了答案,就我所知,它应该是有效的

注意.rb:

has_one :active_comment_relationship, class_name: "Commentrelationship",
                                      foreign_key: "commenter_id",
                                      dependent: :destroy
has_one :supernotice, through: :active_comment_relationship, source: :commentee

表名必须是复数,
:活动注释关系

    Notice.includes(:active_comment_relationships).where(active_comment_relationships: {id: nil} ).limit(50)

在includes部分中,您需要关联名,但在where子句中,您需要表名,表名通常是复数名。在这种情况下,我假设它类似于:

Notice.includes(:active_comment_relationship).where(commentrelationships: {id: nil} ).limit(50)

它是一个has\u one关系,因此它必须是
。包括(:active\u comment\u relationship)
。另外,将
.where(活动注释关系:{id:nil})
更改为
.where(活动注释关系:{id:nil})
没有区别,它仍然会产生相同的错误。@Bazley,只要尝试
.includes(:活动注释关系)
。它必须是这样的,因为表的名称是
active\u comment\u relationships
。这并不取决于你的关系类型use@Bazley,似乎必须是
。包含(:commentrelationships)
,因为这是表的实际名称。它不取决于您使用的关系类型,只需要SQL
内部联接
需要表名,而不是表名relationship@Bazley,我错了,第一部分必须保持原样。尝试在第二部分中使用
.where(commentrelationships:{id:nil})
.where('commentrelationships.id为NULL')
谢谢伊万,你说得对。下面的代码是正确的:
Notice.includes(:active\u comment\u relationship)。where(commentrelationships:{id:nil})。limit(50)
您想写正式答案还是我来写?