0

我无法链接 where 和 Ransack 查询,我收到PG::UndefinedTable错误消息。我正在使用 Rails 6.1.3 和 Ransack 2.4.2。

我已经看到了这个问题:https ://github.com/activerecord-hackery/ransack/issues/1119每个人都同意问题已通过升级到 rails 6.1 得到解决。可悲的是,不适用于我的情况。

我的模型看起来像这样:

class Appointment < ApplicationRecord
  belongs_to :slot
end 

class Slot < ApplicationRecord
  belongs_to :place
end

这个查询

Appointment.joins(slot: [:place])
           .where('slot.date' => Date.today)
           .ransack(slot_place_id_eq: 2)
           .result

返回此错误:

ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR:  invalid reference to FROM-clause entry for table "slots")
LINE 1: ...ointments"."slot_id" WHERE "slot"."date" = $1 AND "slots"."p...
                                                             ^
HINT:  Perhaps you meant to reference the table alias "slot".

生成的查询如下,并且确实slots.place_id不适用于belongs_to。

SELECT appointments.*
FROM appointments 
INNER JOIN slots slot ON slot.id = appointments.slot_id
INNER JOIN places ON places.id = slot.place_id
WHERE slot.date = '2021-06-30' AND slots.place_id = 2

有什么线索吗?

4

1 回答 1

1

我刚刚解决了这个问题:这是一个不同的问题,错误消息与另一个问题相同。

这个 :

.where('slot.date' => Date.today)

应该是这样的:

.where('slots.date' => Date.today)

两个小时的研究,10分钟写一个SO问题,点击发布后30秒自己找到解决方案......

于 2021-06-30T17:31:18.507 回答