Sunspot gem return all result

84 views Asked by At

Sunspot gem return all data like select * from contents

My Model is

  has_many :pictures, :dependent => :destroy
   searchable do
     text :title
     integer :id
     text :pictures do
       pictures.map { |picture| picture.image_file_name }
     end

   end

and my calling section code is

    @search =  Contents.search do

         fulltext params[:q].to_s
         fields(:title)
         fields(:image_file_name)
    end


I am looking for filter result which search from multiple fields and multiple tables
1

There are 1 answers

4
AmitejKalra On

One thing that you need to change in the code is the fields attribute that you are trying to search. In the model you have defined the searchable field as:

text :pictures do
   pictures.map { |picture| picture.image_file_name }
end

Hence, your searchable field is named pictures. But in the controller search, you are trying to search on the field image_file_name, whereas you should be searching on the field pictures.

So, your search code needs to look something like this:

@search =  Contents.search do

         fulltext params[:q].to_s do
           fields(:title, :pictures) 
         end
end

If you still continue facing issues, please also post the code that you are using in the view as well.