Bazley
Bazley

Reputation: 2847

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

Upvotes: 1

Views: 66

Answers (2)

IngoAlbers
IngoAlbers

Reputation: 5802

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)

Upvotes: 1

IvanSelivanov
IvanSelivanov

Reputation: 760

The table name has to be plural, :active_comment_relationships:

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

Upvotes: 0

Related Questions