title: MySQL2: Lost connection to MySQL server during query
body: <p>I am getting the following error when i am trying to use union:</p>
  
  <pre><code>ActionView::Template::Error (Mysql2::Error: Lost connection to MySQL server during query: SELECT  `apps`.* FROM `apps`  WHERE `verve_apps`.`status` IN (1, 2) AND (apps.id IN (SELECT apps.id FROM `apps`  WHERE `apps`.`status` IN (1, 2) AND (app_name LIKE '%b%') UNION SELECT apps.id FROM `verve_apps` INNER JOIN taggings ON taggings.taggable_id = apps.id                   INNER JOIN tags ON tags.id = taggings.tag_id AND taggings.taggable_type = 'App' WHERE `apps`.`status` IN (1, 2) AND (tags.name = 'b') ORDER BY id ASC)) ORDER BY app_name asc LIMIT 10 OFFSET 0)
  </code></pre>
  
  <p><strong>app.rb</strong></p>
  
  <pre><code>class App &lt; ActiveRecord::Base
  
    include ActiveRecord::UnionScope
  
    acts_as_taggable
    attr_accessor: :user_name, :age, :country, tag_list
    scope :tagged_with, lambda { |tag|
      {
        :joins =&gt; "INNER JOIN taggings ON taggings.taggable_id = user.id\
                 INNER JOIN tags ON tags.id = taggings.tag_id AND taggings.taggable_type = 'App'",
        :conditions =&gt; ["tags.name = ?", tag],
        :order =&gt; 'id ASC'
      }
    }
    def self.search(search)
      if search
        union_scope(where('name LIKE ?', "%#{search}%") ,tagged_with(search))
      else
        scoped
      end
    end
  end
  </code></pre>
  
  <p><strong>user_controller.rb</strong></p>
  
  <pre><code>class UserController &lt; ActionController::Base
    def index
      @users = User.search(params[:search]).paginate(:per_page =&gt; per_page, :page =&gt; params[:page])
    end
  </code></pre>
  
  <p><strong>database.yml</strong></p>
  
  <pre><code>pipe_local_development: &amp;pipe_local_development
    adapter: mysql2
    encoding: utf8
    reconnect: true
    database: app_development
    pool: 5
    username: root
    password:
  </code></pre>
  
  <p>I am able to run this method from console without problems.</p>