gpt4 book ai didi

ruby-on-rails - 查找具有关系记录的记录失败:PG::UndefinedTable: ERROR: missing FROM-clause entry for table

转载 作者:行者123 更新时间:2023-11-29 14:14:56 25 4
gpt4 key购买 nike

我有模型 Activity,它 has_one ComputedActivity。我想找到所有已完成处理的事件,这意味着它们在数据库中有一个计算事件。

我写了这个查询

Activity.joins(:computed_activity).where.not(computed_activity: {id: nil})

这给了我错误

ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "computed_activity")
LINE 1: ...tivities"."activity_id" = "activities"."id" WHERE "computed_...
^
: SELECT "activities".* FROM "activities" INNER JOIN "computed_activities" ON "computed_activities"."activity_id" = "activities"."id" WHERE "computed_activity"."id" IS NOT NULL LIMIT $1

是什么导致失败,我该如何解决?

最佳答案

ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: missing FROM-clause entry for table "computed_activity") LINE 1: ...tivities"."activity_id" = "activities"."id" WHERE "computed_...

您应该在 where 子句中包含表名,而不是模型名。所以只要改变

Activity.joins(:computed_activity).where.not(computed_activity: {id: nil})

Activity.joins(:computed_activity).where.not(computed_activities: {id: nil})

关于ruby-on-rails - 查找具有关系记录的记录失败:PG::UndefinedTable: ERROR: missing FROM-clause entry for table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52424019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com