gregbell/active_admin — Issue #994: Corrupted SQL when using order by virtual column
Author: RKushnir
Created: 2012-02-09 12:14:50+00:00
State: open

I'm using scope to restrict the records shown to the user:

scope_to :current_user, :association_method => :managed_reservations

The method `managed_reservations` calls a query

Reservation.joins(:user).select("reservations.*, (users.location_id = #{location_id}) AS local").order('local DESC')

The key problem here is that there's an ordering by a virtual column `local`. For its internal queries ActiveAdmin replaces select statement with '1' (perhaps to check if any records exist), but doesn't replace the order statement which is then referencing a non-existent column and thus fails. The resulting SQL looks like:

SELECT 1 FROM "reservations" INNER JOIN "users" ... ORDER BY local DESC, "reservations".id desc LIMIT 1 OFFSET 0

So I suggest to replace the order statement in such queries too. For now I'm reverting to 0.3.4 which seems to not having this issue.