简体   繁体   中英

Rails sql search through has_one relationship

This query in 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:

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. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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