【问题标题】:PG::UndefinedTable: ERROR: missing FROM-clause entry for table ruby on rails includesPG::UndefinedTable: 错误:缺少表 ruby​​ on rails 的 FROM 子句条目包括
【发布时间】:2018-06-30 03:43:12
【问题描述】:

查询是

Post.includes(:comments).where(:id=>post_ids).select("posts.id,posts.title, posts.description,posts.vote").group('posts.id').order("(count(distinct comments.id)) desc")

它给出了一个错误ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: missing FROM-clause entry for table "comments"

【问题讨论】:

  • 向我们展示您的schema.rb 文件
  • post和cmets之间有_many关系
  • 我猜你的 cmets 表中缺少 post_id
  • 能把你的models定义,log中执行的sql语句和数据库表的定义放上去吗?

标签: ruby-on-rails ruby postgresql include


【解决方案1】:

尝试将includes 更改为joins。因为如果您没有相关模型的条件,它将在单独的查询中加载。 Joins 始终使用单个查询。

【讨论】:

    猜你喜欢
    • 2015-01-22
    • 1970-01-01
    • 2019-02-17
    • 2016-03-24
    • 2015-10-30
    • 2015-12-19
    • 1970-01-01
    • 1970-01-01
    • 2015-02-01
    相关资源
    最近更新 更多