0
votes

Currently I'm migrating search logic to sunspot search engine , I have a query how to implement search in the polymorphic association .

This is the model content

Class Vendor < ActiveRecord::Base has_and_belongs_to_many :specialties end

vendor table fields name,toll_free,credit

class Specialty < ActiveRecord::Base has_and_belongs_to_many :vendors end

specialty table fields name, created_at, updated_at

how to search with the specialty name and display the result where to add searchable for this association.

1

1 Answers

0
votes

I have implemented sunspot search in the model like this

searchable do
    autocomplete :vendor_name, :using => :name
    text  :name,:credit,:toll_free
    text :specialties do |vendor| 
      vendor.specialties.map { |specialty| specialty.name } 
    end  
  end 

In the controller

 @search = Vendor.search(:include=>[:specialties]) do
  fulltext params[:search]
  paginate :page => params[:page], :per_page => 30 
 end
 @vendors = @search.results    

I have followed this site

https://github.com/sunspot/sunspot/wiki/Adding-Sunspot-search-to-Rails-in-5-minutes-or-less

I'm sharing my experience in which I have used sunspot search engine in my project ..