2

I am getting the following error when i am trying to use union:

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)

app.rb

class App < ActiveRecord::Base

  include ActiveRecord::UnionScope

  acts_as_taggable
  attr_accessor: :user_name, :age, :country, tag_list
  scope :tagged_with, lambda { |tag|
    {
      :joins => "INNER JOIN taggings ON taggings.taggable_id = user.id\
               INNER JOIN tags ON tags.id = taggings.tag_id AND taggings.taggable_type = 'App'",
      :conditions => ["tags.name = ?", tag],
      :order => 'id ASC'
    }
  }
  def self.search(search)
    if search
      union_scope(where('name LIKE ?', "%#{search}%") ,tagged_with(search))
    else
      scoped
    end
  end
end

user_controller.rb

class UserController < ActionController::Base
  def index
    @users = User.search(params[:search]).paginate(:per_page => per_page, :page => params[:page])
  end

database.yml

pipe_local_development: &pipe_local_development
  adapter: mysql2
  encoding: utf8
  reconnect: true
  database: app_development
  pool: 5
  username: root
  password:

I am able to run this method from console without problems.

Mano
  • 979
  • 1
  • 18
  • 36
  • 1
    Did you intend for `where('name LIKE ?', "%#{search}%") + tagged_with(search)` to be a union, or is it supposed to be a single select, which would be given by `where('name LIKE ?', "%#{search}%").tagged_with(search)`? – Alexa Y Oct 22 '15 at 13:21
  • Possible duplicate of [Lost connection to MySQL server during query](http://stackoverflow.com/questions/1884859/lost-connection-to-mysql-server-during-query) – Drenmi Oct 22 '15 at 14:26
  • @BenY http://stackoverflow.com/questions/33259409/rails-add-two-scope-with-active-record-result/33260779 I think so... – markets Oct 22 '15 at 15:04
  • @BenY I have updated my question. Kindly take a look at it. – Mano Oct 23 '15 at 06:01

0 Answers0