hyperrjas
hyperrjas

Reputation: 10744

No field configured for Model with name 'image_filename'

I have a Model with 2 atrributes:

:image_filename 
:yt_video_id

I have this code in my controller:

def index
   @search = Model.solr_search do |s|
   s.fulltext params[:search]
   s.paginate :page => params[:page], :per_page => 2
   s.with(:image_filename || :yt_video_id)
   end
   @model = @search.results
   respond_to do |format|
    format.html # index.html.erb
  end
 end

in my model.rb Model I have this in searchable:

searchable do
    string :image_filename, :yt_video_id
  end

I want filter :image_filename OR :yt_video_id any are not "nil". I mean, both attributes must have a mandatory value.

but I get the error:

Sunspot::UnrecognizedFieldError in ModelsController#index

No field configured for Model with name 'image_filename'

Upvotes: 0

Views: 2859

Answers (1)

hyperrjas
hyperrjas

Reputation: 10744

The problem was fixed with the following steps:

(This solution works fine for me. I hope this solution can help you too.)

In model.rb you can not write this syntax:

searchable do
    string :image_filename, :yt_video_id
  end

You must write this syntax:

searchable do
      string :image_filename
      string :yt_video_id
     end

In your models_controller.rb in index action:

def index
   @search = Model.solr_search do |s|
   s.fulltext params[:search]
   s.paginate :page => params[:page], :per_page => 2
   s.any_of do
      without(:image_filename, nil)
      without(:yt_video_id, nil)
     end
   end
   @model = @search.results
   respond_to do |format|
    format.html # index.html.erb
   end
 end

I have used the any_of method.

To combine scopes using OR semantics, use the any_of method to group restrictions into a disjunction:

Sunspot.search(Post) do
  any_of do
    with(:expired_at).greater_than(Time.now)
    with(:expired_at, nil)
  end
end

You can see in https://github.com/sunspot/sunspot/wiki/Scoping-by-attribute-fields

Upvotes: 2

Related Questions