Search code examples
ruby-on-railsrubyruby-on-rails-3sunspot

search with sunspot in nested objects


I have 2 model

The user.rb model

Class User
 include Mongoid::Document
 has_many :posts, dependent: :destroy, :autosave => true
 accepts_nested_attributes_for :posts
 field :name
 attr_accessible :name
end

The post.rb model

Class Post
 include Mongoid::Document
 include Sunspot::Mongo
 belongs_to :user
 field :name
 attr_accessible :name

 searchable do
   text :name, :boost => 2.0
   time :created_at
 end
end

In my controller:

def posts
@user = User.find(params[:id]) 
@search = Post.solr_search do |s|
 s.fulltext params[:search]
 s.keywords params[:search]
 s.paginate :page => params[:page], :per_page => 50
 end
  @posts = @search.results
   respond_to do |format|
    format.html { render :layout => nil}# panel.html.erb
   end
end

With this sunspot search in all the Posts in database.

I want that sunspot only search in post that belongs to @user = User.find(params[:id]).

How can I do it?

The problem was fixed

You can seee the fix in sunspot return all results for a model


Solution

  • Copying the answer from the comments in order to remove this question from the "Unanswered" filter:

    The problem was fixed

    You can seee the fix in sunspot return all results for a model

    ~ answer per hyperrjas