1

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
Community
  • 1
  • 1
Bazley
  • 2,699
  • 5
  • 36
  • 61

2 Answers2

1

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)
IngoAlbers
  • 5,722
  • 6
  • 31
  • 45
0

The table name has to be plural, :active_comment_relationships:

    Notice.includes(:active_comment_relationships).where(active_comment_relationships: {id: nil} ).limit(50)
IvanSelivanov
  • 710
  • 5
  • 15
  • It's a has_one relationship, so it has to be `.includes(:active_comment_relationship)`. Also, changing `.where(active_comment_relationship: {id: nil})` to `.where(active_comment_relationships: {id: nil})` makes no difference, it still produces the same error. – Bazley May 20 '15 at 12:32
  • @Bazley, just try `.includes(:active_comment_relationships)`. It has to be like this because the name of the table is `active_comment_relationships`. It does not depend on which type of relationship you use – IvanSelivanov May 20 '15 at 12:45
  • @Bazley, seems like it has to be `.includes(:commentrelationships)`, because this is the actual name of the table. It does not depend on the type of relationship You use, it's just SQL `INNER JOIN` that needs a name of a table, not a relationship – IvanSelivanov May 20 '15 at 12:51
  • @Bazley, I was wrong, first part has to be as it is. try using `.where(commentrelationships: {id: nil} )` or `.where('commentrelationships.id IS NULL')` in the second part – IvanSelivanov May 20 '15 at 13:03
  • Thank you Ivan, you are right. The following code is correct: `Notice.includes(:active_comment_relationship).where(commentrelationships: {id: nil} ).limit(50)` Do you want to write the official answer or shall I? – Bazley May 20 '15 at 13:43