title: PG::SyntaxError when ActiveRecord has default order and sub-scope use union search
body: <p>I have three models Project, Project::Task, and Project::Task::Video look like following:</p>
  
  <pre><code>class Project &lt; ActiveRecord::Base
      has_many :tasks
      has_many :videos, class_name: 'Project::Task::Video', through: :tasks
  end
  
  class Project::Task &lt; ActiveRecord::Base
      default_scope { order(id: :asc) }
      belongs_to :app
      has_many :videos, source: :videos
  end
  
  class Project::Task::Video &lt; ActiveRecord::Base
      acts_as_taggable
  
      belongs_to :task
  
      def self.search(keyword)
          result_1 = Project::Task::Video.tagged_with(something)
          result_2 = Project::Task::Video.join(something).where(something)
          total_result = Project::Task::Video.from("(#{result_1.to_sql} UNION #{result_2.to_sql}) AS project_task_videos")
      end
  end
  </code></pre>
  
  <p>When I want to do some data searching from database, I wrote following code:</p>
  
  <pre><code>app.videos.search('stuff')
  </code></pre>
  
  <p>Then I get error message:</p>
  
  <pre><code>ActiveRecord::StatementInvalid in Projects#videos
  
  PG::SyntaxError: ERROR:  syntax error at or near "UNION"
  LINE 1: ...deleted_at" IS NULL ORDER BY "project_tasks"."id" ASC UNION SELE...
  </code></pre>
  
  <p>But if I remove default order scope <code>default_scope { order(id: :asc) }</code> from Project::Task, these code just work fine.</p>
  
  <p>And the original result sql code combines many joins, so union query is necessarily.  </p>
  
  <p>Can I just use default order scope with sub-model union together? or there is other way to make default order for <code>tasks</code>?</p>
  
  <p>Please Help me figue it out.</p>
  
  <p>-</p>
  
  <p>Additional Note:
  My full sql message has taskitem layer, may looks a little more complicated.</p>
  
  <pre><code>: SELECT  "app_task_taskitem_videos".* FROM (SELECT "app_task_taskitem_videos".* FROM "app_task_taskitem_videos" INNER JOIN "app_task_taskitems" ON "app_task_taskitem_videos"."taskitem_id" = "app_task_taskitems"."id" INNER JOIN "app_tasks" ON "app_task_taskitems"."task_id" = "app_tasks"."id" WHERE "app_tasks"."app_id" = 1 AND "app_task_taskitems"."deleted_at" IS NULL AND "app_task_taskitems"."deleted_at" IS NULL ORDER BY "app_tasks"."id" ASC UNION SELECT "app_task_taskitem_videos".* FROM "app_task_taskitem_videos" INNER JOIN "app_task_taskitems" ON "app_task_taskitem_videos"."taskitem_id" = "app_task_taskitems"."id" INNER JOIN "app_tasks" ON "app_task_taskitems"."task_id" = "app_tasks"."id" WHERE "app_tasks"."app_id" = 1 AND "app_task_taskitems"."deleted_at" IS NULL AND "app_task_taskitems"."deleted_at" IS NULL ORDER BY "app_tasks"."id" ASC) AS app_task_taskitem_videos INNER JOIN "app_task_taskitems" ON "app_task_taskitem_videos"."taskitem_id" = "app_task_taskitems"."id" INNER JOIN "app_tasks" ON "app_task_taskitems"."task_id" = "app_tasks"."id" WHERE "app_tasks"."app_id" = $1 AND "app_task_taskitems"."deleted_at" IS NULL AND "app_task_taskitems"."deleted_at" IS NULL ORDER BY "app_tasks"."id" ASC, "app_task_taskitem_videos"."id" DESC LIMIT $2 OFFSET $3
  </code></pre>