ruby-on-rails Rails PG::未定义表格:错误:缺少表的FROM子句条目

j9per5c4  于 2022-11-19  发布在  Ruby
关注(0)|答案(1)|浏览(172)

我有模特

class Offer < ActiveRecord::Base
  belongs_to :agency
end

class Agency < ActiveRecord::Base
  has_many :offers
end

当我提出这样的要求时-一切都OK

@offers = Offer.with_state(:confirmed).
  includes(:destination, :cruise_line, :ship).
  paginate(per_page: 10, page: params[:page]).decorate

但是我只想选择属于active代理的报价(agencies表中的state列),所以我尝试这样做:

@offers = Offer.with_state(:confirmed).
  includes(:destination, :cruise_line, :ship).
  joins(:agency).
  where(agency: {state: 'active'}).
  paginate(per_page: 10, page: params[:page]).decorate

这样做之后,我得到了错误PG::UndefinedTable: ERROR: missing FROM-clause entry for table "agency"。我的代码有什么问题?
查询返回以下错误和sql:

PG::UndefinedTable: ERROR: missing FROM-clause entry for table "agency" LINE 1: ...id" WHERE ("offers"."state" IN ('confirmed')) AND "agency"."... ^ : 
SELECT "offers"."id" AS t0_r0, "offers"."name" AS t0_r1, "offers"."destination_id" AS t0_r2, "offers"."cruise_line_id" AS t0_r3, "offers"."ship_id" AS t0_r4, "offers"."departure_date" AS t0_r5, "offers"."departure_port_id" AS t0_r6, "offers"."arrival_date" AS t0_r7, "offers"."arrival_port_id" AS t0_r8, "offers"."flight_price" AS t0_r9, "offers"."bonus" AS t0_r10, "offers"."itinerary" AS t0_r11, "offers"."board_language_id" AS t0_r12, "offers"."agency_landing_page" AS t0_r13, "offers"."benefits" AS t0_r14, "offers"."inner_price" AS t0_r15, "offers"."inner_price_normal" AS t0_r16, "offers"."outer_price" AS t0_r17, "offers"."outer_price_normal" AS t0_r18, "offers"."balcony_price" AS t0_r19, "offers"."balcony_price_normal" AS t0_r20, "offers"."suite_price" AS t0_r21, "offers"."suite_price_normal" AS t0_r22, "offers"."lucky_price" AS t0_r23, "offers"."lucky_price_normal" AS t0_r24, "offers"."valid_from" AS t0_r25, "offers"."valid_till" AS t0_r26, "offers"."created_at" AS t0_r27, "offers"."updated_at" AS t0_r28, "offers"."description" AS t0_r29, "offers"."agency_id" AS t0_r30, "offers"."state" AS t0_r31, "destinations"."id" AS t1_r0, "destinations"."name" AS t1_r1, "destinations"."created_at" AS t1_r2, "destinations"."updated_at" AS t1_r3, "cruise_lines"."id" AS t2_r0, "cruise_lines"."name" AS t2_r1, "cruise_lines"."created_at" AS t2_r2, "cruise_lines"."updated_at" AS t2_r3, "ships"."id" AS t3_r0, "ships"."name" AS t3_r1, "ships"."picture" AS t3_r2, "ships"."cruise_line_id" AS t3_r3, "ships"."created_at" AS t3_r4, "ships"."updated_at" AS t3_r5 
FROM "offers" 
INNER JOIN "agencies" ON "agencies"."id" = "offers"."agency_id" 
LEFT OUTER JOIN "destinations" ON "destinations"."id" = "offers"."destination_id" 
LEFT OUTER JOIN "cruise_lines" ON "cruise_lines"."id" = "offers"."cruise_line_id" 
LEFT OUTER JOIN "ships" ON "ships"."id" = "offers"."ship_id" 
WHERE ("offers"."state" IN ('confirmed')) AND "agency"."state" = 'active' 
LIMIT 10 OFFSET 0
35g0bw71

35g0bw711#

错误

ERROR: missing FROM-clause entry for table "agency"

......应该会提示您在查询中的某个地方错误地使用了agency作为表名,而没有使用复数形式。但是您到底是在哪里这样做的呢?
工作和非工作代码段之间的唯一区别是以下几点:

joins(:agency).
where(agency: {state: 'active'}).

这两行都指向agency .嗯,没有简单的路径,需要检查这两行.
joins部分负责生成查询的INNER JOIN部分,如果查看生成的SQL,它只使用agencies,这是表的实际名称。由于遵循Rails的命名约定,因此指向知道其表名的正确模型,这样就可以工作了。
但是,where部分被破坏了。您使用的这个特殊的“子散列”条件形式期望散列键是一个表名,并在查询中直接使用它。将其替换为表的实际名称,您应该很好:

where(agencies: {state: 'active'})

#merge的方式

还有另一种方法来完成同样的壮举,它涉及,而不是#where#merge方法,* 合并 * 一个关系的条件到另一个条件:

merge( Agency.where(state: 'active') )

即使这两个关系在不同的模型上也是有效的,这对于过滤连接的记录很方便,这正是您正在做的。
此外,它还允许您使用模型类的一些功能。
特别是,范围:

# Inside Agency
scope :active, -> { where(state: 'active') }

# Somewhere else
merge(Agency.active)

另外,学习另一个模型的表名是什么。在#where中,你必须在查询中指定表名,可能从外部进入模型的数据库持久性范围。使用#merge,你服从模型,希望它是唯一的真实来源。

相关问题