【问题标题】:PG::UndefinedTable: ERROR: missing FROM-clause -- Ordering an associated record with "includes" RailsPG::UndefinedTable: 错误:缺少 FROM 子句——使用“包含”Rails 订购关联记录
【发布时间】:2015-02-01 05:31:53
【问题描述】:

我有一个应用程序,可以在其中显示当前用户 (posts.current_user) 的帖子。我想通过comment.date排序显示最近评论最多的帖子。它似乎不想这样做......我不断得到:

PG::UndefinedTable: 错误:缺少表“注释”的 FROM 子句条目

我的控制器

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

我已经尝试过加入并包含一个我似乎无法破解的。谢谢。

【问题讨论】:

    标签: ruby-on-rails join include


    【解决方案1】:

    试试:

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

    解释:

    • 您需要加入
    • 您必须按照顺序引用表的名称,而不是关联的名称

    【讨论】:

    • 我明白了:PG::UndefinedTable: 错误:缺少表“评论”第 1 行的 FROM 子句条目:...id" WHERE "posts"."user_id" = $1 ORDER BY comment .d...
    • 啊...等等...我错过了 cmets 中的“s”!就是这样。谢谢!
    • "按顺序你必须引用表的名称,而不是关联的名称"非常重要,谢谢!
    猜你喜欢
    • 2015-10-30
    • 2015-01-22
    • 2018-06-30
    • 1970-01-01
    • 2019-02-17
    • 2016-03-24
    • 2015-12-19
    • 1970-01-01
    • 2014-09-25
    相关资源
    最近更新 更多