Reputation: 1203
Simple question, How do I search multiple models using Sunspot?...I'm using Sunspot with Rails, and I can't seem to find how I search across multiple models. Do I need to create a separate controller or can I use the Gear Controller with the index action?
Thanks for the help.
(side note: I found a question on stackoverflow very similar to this but they didn't post their code..so I apologize for any redundancy.)
I have the following in my view:
<div class="side_bar_search">
<%= form_tag gears_path, :method => :get do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
<% end %>
</div>
and the following models
Gear
class Gear < ActiveRecord::Base
attr_accessible :title, :size, :price, :sub_category_id, :user_id, :image, :image_a, :remote_image_url
belongs_to :user
belongs_to :sub_category
has_one :category, :through => :sub_category
has_many :comments, :dependent => :destroy
require 'carrierwave/orm/activerecord'
mount_uploader :image, GearpicUploader
mount_uploader :image_a, GearpicUploader
searchable do
text :title, :size, :price #need to add sub-category, and User Name.
end...
User
class User < ActiveRecord::Base
attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :userimage, :remove_userimage
has_secure_password
has_many :gears
has_many :comments, :dependent => :destroy
has_one :store, :dependent => :destroy
before_save :create_remember_token
require 'carrierwave/orm/activerecord'
mount_uploader :userimage, UserpicUploader
searchable do
text :first_name, :last_name
end...
Gears Controller
class GearsController < ApplicationController
def index
@search = Gear.search do
fulltext params[:search]
paginate(page: params[:page])
end
@gears = @search.results
end...
Upvotes: 3
Views: 2608
Reputation: 10208
Also, a gem that can be handy for this is
https://github.com/toptierlabs/acts_as_fulltextable
It doesn't have any dependency with the search server.
Upvotes: 0