Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PG::UndefinedTable: ERROR: missing FROM-clause -- Ordering an associated record with "includes" Rails

I have an app where I show posts by a current user (posts.current_user). I would like to show the most posts that have been most recently commented on by ordering by comment.date. It doesn't seem to want to do that...I keep getting:

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

My Controller

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

I have tried joins and includes an I can't seem to crack this. Thank you.

like image 934
NothingToSeeHere Avatar asked Dec 03 '14 15:12

NothingToSeeHere


1 Answers

Try:

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

Explanation:

  • you need a join
  • in the order you must reference the table's name, not the association's name
like image 106
apneadiving Avatar answered Sep 30 '22 11:09

apneadiving