简体   繁体   中英

PG::Error: ERROR: column reference “status” is ambiguous in active_admin

Using rails 3.2 with active_admin and seeing PG::Error: ERROR: column reference "status" is ambiguous when using a custom filter on active_admin in Rents.rb :

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

The error points to:

: 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'):

it's interesting that the above has status = 'paid' since I'm not sure why its using that as a filter.

models

Rent.rb
belongs_to :travel

Travel.rb
has_one :car

and both rents table and travels table have a status attribute.

I've seen Lucas' answer but if this is a rails app, the SQL should be generated by the application, not hardcoded. Therefore changing the SQL directly is not the solution.

Instead, I would suggest you find the code that is adding the "paid" filter and modify it to declare the relevant model name.

Somewhere you probably have a scope:

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

change that to (for example):

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

You need to chose wich table you want your attribute to select, or use both

eg

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'

or if you require both:

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'

Your "status" column is ambiguous. Because SQL can't understand which one table's column you want. Rent.status or Travels.status that SQL can not understand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM