PG ::错误:错误:列引用“status”在active_admin中不明确

时间:2014-08-04 09:10:05

标签: ruby-on-rails postgresql activeadmin

rails 3.2的{​​{1}}上使用自定义过滤器时,active_adminPG::Error: ERROR: column reference "status" is ambiguous一起使用并看到active_admin

Rents.rb

错误指向:

filter :travel_car_brand, as: :string
filter :travel_car_model, as: :string

有趣的是,上面有: SELECT COUNT(DISTINCT "rents"."id") FROM "rents" LEFT OUTER JOIN "travels" ON "travels"."id" = "rents"."travel_id" LEFT OUTER JOIN "cars" ON "cars"."travel_id" = "travels"."id" WHERE ("cars"."brand" ILIKE '%mazda%') AND ("startDate" > '2014-08-04 10:15:14 +0200' and status = 'paid'): ,因为我不确定为什么将它用作过滤器。

模型

status = 'paid'

并且租借表和旅行表都有Rent.rb belongs_to :travel Travel.rb has_one :car 属性。

3 个答案:

答案 0 :(得分:4)

我已经看过Lucas' answer但是如果这是一个rails应用程序,SQL应该由应用程序生成,而不是硬编码。因此,直接更改SQL不是解决方案。

相反,我建议您找到添加"付费"的代码。过滤并修改它以声明相关的模型名称。

某处你可能有一个范围:

scope :paid, where(status: 'paid')

将其更改为(例如):

scope :paid, where("model.status = 'paid'")

答案 1 :(得分:1)

您需要选择要让属性选择的表格,或同时使用两者

e.g。

SELECT
COUNT(DISTINCT "rents"."id")
FROM "rents"
LEFT OUTER JOIN "travels" ON "travels"."id" = "rents"."travel_id"
LEFT OUTER JOIN "cars" ON "cars"."travel_id" = "travels"."id"
WHERE ("cars"."brand" ILIKE '%mazda%')
AND ("startDate" > '2014-08-04 10:15:14 +0200') 
AND rents.status = 'paid'

或如果您同时需要:

SELECT
COUNT(DISTINCT "rents"."id")
FROM "rents"
LEFT OUTER JOIN "travels" ON "travels"."id" = "rents"."travel_id"
LEFT OUTER JOIN "cars" ON "cars"."travel_id" = "travels"."id"
WHERE ("cars"."brand" ILIKE '%mazda%')
AND ("startDate" > '2014-08-04 10:15:14 +0200')
AND rents.status = 'paid'
AND travels.status = 'paid'

答案 2 :(得分:0)

你的状态"专栏含糊不清。因为SQL无法理解您想要哪一个表的列。 SQL无法理解的Rent.status或Travels.status。