当我尝试使用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:
我可以在没有问题的情况下从控制台运行此方法。