will_paginate用于在rails中使用sunspot 2.2.2的组查询

时间:2016-01-11 08:02:00

标签: ruby-on-rails ruby sunspot sunspot-solr

我在我的rails应用程序中使用sunspot 2.2.2来搜索结果, 我有这个代码用于在我的模型中进行分组:

def self.search_products(params, currency = nil, page_uri = nil)
  search_products = Sunspot.search(VariantPrice) do
   group :code do 
    if params[:gallery_order].present?
      order_by :price, params[:gallery_order].downcase.to_sym
    elsif params[:new_arrival].present? || params[:name] == "new-arrivals"
      order_by :product_created_at, :desc
    else
      if params[:fashion_type] == "fashion"
        order_by :price, :asc
      elsif params[:sort] != "lowhigh"
        order_by :price, :asc
      else
        order_by :price, :asc
      end
    end
    limit 1
  end
end

我的控制器中有这个代码:

variant_prices = Product.search_products(params, @currency, request.original_fullpath)

@variant_prices = []
variant_prices.group(:code).groups.each do |group|
  group.results.each do |result|
    @variant_prices << result       
  end
end
@variant_prices = @variant_prices.paginate(:page => params[:page], :per_page => PER_PAGE_VALUE)
@variant_prices_count = variant_prices.group(:code).total

现在我得到的是@variant_prices_count的预期计数,在我的情况下是1400,但我得到@variant_prices计数为60,这在我的情况下是错的,在这里我期望得到1400.然后我想要用这个结果分页。无法理解它是否会引发问题或其他问题。 救命啊!

1 个答案:

答案 0 :(得分:1)

您也可以通过Total entries

从分页实例中获取1400

通过这种方式将count替换为total_entries

@variant_prices = @variant_prices.paginate(:page => params[:page], :per_page => PER_PAGE_VALUE)
@variant_prices.total_entries#it will return toal entries 1400