belcortes
belcortes

Reputation: 1

Rails Sunspot Search by boolean name

I'm using Sunspot Search with Rails 4 and it's been working great, there's only one thing I haven't been able to figure out, and after days of banging my head against the wall I decided to ask here.

I have a model User with a few boolean attributes. I want to make it so that when I search the attribute name the search results will return all objects with that attribute if the attribute is true.

The attributes that are string and have an actual value besides 'true' and 'false' work great with the search.

My user model (user.rb):

class User << ActiveRecord::Base

 searchable do
  text :zip, :name, :email
  boolean :cpr
  boolean :first_aid
  boolean :survival_kit
 end

end

My Search Controller (search_controller.rb). I'm making two models searchable:

class SearchController << ApplicationController

 def search

  @search = Sunspot.search(User, CoolingCenter) do
   fulltext params[:search]
  end

  @results = @search.results

  respond_to do |format|
   format.json { render json: @results}
   format.html { render '_results', layout: true }
  end

 end

end

So for example, if I search 'cpr', the search results would be all users with the attribute :cpr = true.

Thanks a lot for the help!

Upvotes: 0

Views: 187

Answers (1)

Vitaly
Vitaly

Reputation: 161

Try to add conditions after fulltext params[:search] line:

fulltext params[:search]

with :cpr, true

with :first_aid, true

with :survival_kit, true

Upvotes: 0

Related Questions