简体   繁体   English

Rails SQL搜索has_one关系

[英]Rails sql search through has_one relationship

This query in notices_controller.rb: 在notices_controller.rb中的此查询:

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

is producing this error: 正在产生此错误:

PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "active_comment_relationship"

I can't see what's wrong with the code. 我看不到代码有什么问题。 I've looked at answers like this and as far as I can tell it should be working. 我看像回答这个而据我可以告诉它应该工作。

notice.rb: notice.rb:

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

In the includes part you need the association name, but in the where clause you need the table name, which usually is the pluralized name. 在包括部分中,您需要关联名称,但是在where子句中,您需要表名称,该名称通常是复数名称。 In this case I assume it is something like: 在这种情况下,我假设它类似于:

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

表名必须是复数, :active_comment_relationships

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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