Skip to content

Instantly share code, notes, and snippets.

@tabishiqbal
Created September 2, 2016 14:17
Show Gist options
  • Save tabishiqbal/f9da928bd85fe22bc36e442b7df0764f to your computer and use it in GitHub Desktop.
Save tabishiqbal/f9da928bd85fe22bc36e442b7df0764f to your computer and use it in GitHub Desktop.
pg_search example
<div class="wrapper">
<div class="row">
<div class="col-md-12">
<div class="box">
<% unless @products.blank? %>
<%= render 'search/products' %>
<% end %>
<% unless @vendors.blank? %>
<%= render 'search/vendors' %>
<% end %>
</div> <!-- end box-content -->
</div>
</div>
</div>
class CreatePgSearchDocuments < ActiveRecord::Migration
def self.up
say_with_time("Creating table for pg_search multisearch") do
create_table :pg_search_documents do |t|
t.text :content
t.belongs_to :searchable, :polymorphic => true, :index => true
t.timestamps null: false
end
end
end
def self.down
say_with_time("Dropping table for pg_search multisearch") do
drop_table :pg_search_documents
end
end
end
class Product < ActiveRecord::Base
belongs_to :vendor
include PgSearch
pg_search_scope :search_including_tags,
:against => [:model, :description],
:using => {
:tsearch => {:prefix => true}
}
end
end
class ProductsController < ApplicationController
def index
if params[:search]
elsif params[:id]
@products = Product.find(params[:id])
else
@products = Product.all.order("created_at DESC").paginate(:page => params[:page], :per_page => 15)
@product = Product.first
end
end
end
Rails.application.routes.draw do
get '/search', to: 'search#index'
end
class SearchController < ApplicationController
def new
@products = Product.search_including_tags(params[:query]).paginate(:page => params[:page], :per_page => 15)
@vendors = Vendor.search_including_tags(params[:query]).paginate(:page => params[:page], :per_page => 15)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment