81947

sunspot return all results for a model

I have in my controller this:

def boards @user = User.find_by_slug(params[:id]) @search = @user.boards.solr_search do |s| s.fulltext params[:search] s.keywords params[:search] s.order_by :created_at, :desc s.paginate :page => params[:page], :per_page => 1 end @boards = @search.results respond_to do |format| format.html { render :layout => nil}# panel.html.erb format.json { render json: @boards } format.js end end

In my view:

<table id="body_object"> <% for board in @boards %> <tr class="attributes"> <td> <%= board.id %> </td> <td> <%= board.name %> </td> <td> <%= board.description %> </td> </tr> <% end %> </table> <div id="content_pagination"> <%= paginate @boards, :remote => :true %> </div>

@user.boards in controller are every boards that belongs to user.

But I get every boards like Boards.all.

I want get only every boards that belongs to user.

I have tried with @user.boards but I have that paginate an array like sth:

@boards = Kaminari.paginate_array(@user.boards).page(params[:page]).per(1)

How can I fix this problem for sunspot?

Edited: Added Console Test

1.9.2-p290 :094 > Board.all.size #I get count boards for Board.all => 4 1.9.2-p290 :095 > user = User.first #I get the first 1.9.2-p290 :096 > user.boards.size #I get count for boards belongs to user => 2 1.9.2-p290 :098 > user.boards.solr_search.total # This is the problem :O The result must be 2 => 4

Answer1:

Problem was fixed

The fix: To Model searchable block you have add the parent for this board <strong>:user_id</strong>, string if you have mongodb database and integer if you have sql database:

#search searchable do text :name string :user_id end

In controller for this case I have add s.with(:user_id, @user.id) and replace @user.boards with the Model name like:

def boards @user = User.find_by_slug(params[:id]) @search = Board.solr_search do |s| s.fulltext params[:search] s.keywords params[:search] s.order_by :created_at, :desc s.with(:user_id, @user.id) s.paginate :page => params[:page], :per_page => 1 end @boards = @search.results respond_to do |format| format.html { render :layout => nil}# panel.html.erb format.json { render json: @boards } format.js end end

Now works fine :D. Thank you

Answer2:

The problem seems to be with SOLR returning more than you expect -- the rest of the code is fine as far as I can see.

First verify that the params[:search] has the value you expect (check the development.log), and if so, you should run rails console and try the search manually.

Recommend

  • Unable to pushViewController iOS
  • Template is missing error even if I have done a redirect_to in the controller
  • Getting rid of node-jsx
  • Rails has_many :through association: save instance into join table
  • Gatttool non-interactive mode --char-write
  • NSOpenPanel's setDirectoryURL doesn't work on Lion
  • Windows Forms switch between Panels
  • Odd function behaviour with Tkinter
  • how to add semantic ui in a rails app?
  • Extjs, handling success or failure when doing a standard submit in a form
  • CSS Grid, position absolute an element in a css grid item: IMPOSSIBLE
  • Why does the font in these TD elements render at different sizes?
  • garbled css name when styling within UiBinder
  • How to open html table in xls on click of a button
  • how to get data attributes of dynamically generated element
  • How to disable all widgets inside Panel or inside Composite?
  • ListItem.Attributes.Add not working
  • Loading .coffee files via a view in Rails
  • How to change the font size of a single index for UISegmentedControl?
  • Play WS (2.2.1): post/put large request
  • Read a local file using javascript
  • Different response to non-authenticated users and AJAX calls
  • Delete MySQLi record without showing the id in the URL
  • Apache 2.4 - remove | delete | uninstall
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Unanticipated behavior
  • Comma separated Values
  • Proper way to use connect-multiparty with express.js?
  • Trying to get generic when generic is not available
  • Java static initializers and reflection
  • embed rChart in Markdown
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Is it possible to post an object from jquery to bottle.py?
  • unknown Exception android
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal