Search for multiple models at once in Sunspot / Solr for Rails

A simple question: how do I search for multiple models using Sunspot? ... I use Sunspot with Rails, and I cannot find how I view multiple models. Do I need to create a separate controller or can I use Gear Controller with index action?

Thanks for the help.

(side of note: I found the stackoverflow question very similar to this, but they did not post their code. Therefore, I apologize for any redundancy.)

I have the following in my opinion:

<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

Tackle

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...                    

Controller gears

class GearsController < ApplicationController
  def index
    @search = Gear.search do
        fulltext params[:search]
        paginate(page: params[:page])
    end

    @gears = @search.results
  end...
+3
source share
2 answers
Sunspot.search [Gear, User] do
  ....
end
+11
source

In addition, a gem that may be useful for this is

https://github.com/toptierlabs/acts_as_fulltextable

.

0

All Articles