Created
September 2, 2016 14:17
-
-
Save tabishiqbal/f9da928bd85fe22bc36e442b7df0764f to your computer and use it in GitHub Desktop.
pg_search example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Product < ActiveRecord::Base | |
belongs_to :vendor | |
include PgSearch | |
pg_search_scope :search_including_tags, | |
:against => [:model, :description], | |
:using => { | |
:tsearch => {:prefix => true} | |
} | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rails.application.routes.draw do | |
get '/search', to: 'search#index' | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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