elasticsearch-rails: Pagination + records + additional scopes broken

The pagination support breaks down if you chain additional scopes on after calling records (e.g. to eager load relations and avoid N+1 queries). You end up getting NoMethodError: undefined methodtotal_count’ for [MODEL NAME]. Calling.page(params[:page]).per(x)` adds back in the appropriate pagination methods but because we end up doing an explicit fetch of just the IDs in the Elasticsearch result page we get a total_count that is equal to the original per_page instead of the actual total_count in Elasticsearch’s result set.

Model.search('foo').page(1).per(10).records.total_count => a big number
Model.search('foo').page(1).per(10).records.includes(:relationship).total_count => Undefined method error
Model.search('foo').page(1).per(10).records.page(1).per(10).total_count => 10

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 29 (7 by maintainers)

Most upvoted comments

Actual problem, my solution:

@results_ = Model.search('query').page(params[:page]).records
@results  = @results_.includes(:profile)
@results.each do |record|
...
end

= will_paginate @results_