PG :: UndefinedTable: ERROR: missing FROM clause - order related record with "include" Rails

I have an application where I display the messages of the current user (posts.current_user). I would like to show most of the posts that were recently commented out when ordering comment.date. He doesn't seem to want to do this ... I keep getting:

PG :: UndefinedTable: ERROR: missing FROM-clause entry for comment table

My controller

def_index @posts = current_user.posts.includes(:comment).order("comment.date ASC").includes(:image) end 

I tried to unite and turned it on, it seems I can’t crack it. Thanks.

+5
source share
1 answer

Try:

  @posts = current_user.posts.joins(:comment).order("comments.date ASC").includes(:image) 

Explanation:

  • you need to join
  • in the order in which you must refer to the table name, not the association name
+13
source

Source: https://habr.com/ru/post/1208260/


All Articles