Multiple model search in rails with solr

lrSearching is much easier if you use solr or elastic search, no need to write complex database queries.

If you want to integrate solr with your rails application means there is a gem available

Add to Gemfile:

gem 'sunspot_rails'
gem 'sunspot_solr' # optional pre-packaged Solr distribution for use in development

Bundle it!

bundle install

Generate a default configuration file:

rails generate sunspot_rails:install

If sunspot_solr was installed, start the packaged Solr distribution with:

 bundle exec rake sunspot:solr:start

Then add the following code in model file which you going to search

searchable do
  text :<field_name>

In your controller queries

@search = Project.solr_search do
fulltext params[:search]
@projects = @search.results

Thats all, if you pass search params from your view file, then solr will give you the relevant projects.

Now I will show you how to search multi models in same querie,

simply add the same

searchable do
 text :<field_name>

In model files which you need to search, Then in your controller

@search =,Comment, File) do |s|

@result = @search.result

will give you the result from project, file and comment models