Overriding Active Query construct in Rails
I have the following trying to pull out a selection of Model instances from my DB with this:
@coasters = Coaster.joins(:park).order('parks.name ASC').order('coasters.name ASC')
Near the end of my index action I need to override the order part of the above query so I do the following:
@coasters = @coasters.except(:joins).reorder('order ASC')
But I get this error:
PG::UndefinedTable: ERROR: missing FROM-clause entry for table "parks" LINE 1: SELECT "coasters".* FROM "coasters" ORDER BY parks.name AS... ^ : SELECT "coasters".* FROM "coasters" ORDER BY parks.name ASC, coasters.name ASC, "order" ASC LIMIT 25 OFFSET 0
Any ideas as to how I can solve it?
You should consider using the lazy load feature and do not perform any order at all, before you are sure on what sorting you need.
@coasters = Coaster.where(...).joins(...)
@coasters = @coasters.order(...)