How to paginate sunspot in this manner?

我只是一个虾纸丫 提交于 2019-12-21 21:52:57

问题


I have the following model associations:

class Product < ActiveRecord::Base
  has_many :prices

class Price < ActiveRecord::Base
  belongs_to :product

Here is my controller

class SearchController < ApplicationController

  # Using Sunspot here.
  def index
    @search = Product.search do
      fulltext params[:search]
      paginate(:per_page => 5, :page => params[:page])
    end
    @products = @search.results
  end
end

and then my view:

<%= will_paginate @products %>
<% @products.each do |product| %>
    <%= product.name %>
  <% prices.each do |price| %>
    <%= price.price %>
  <% end %>
<% end %>

Lets say I have 10 Products that each have 20 different Prices. My goal is to show 5 Products per page but have the Prices run over to the next page if the amount of Prices on a page exceeds a maximum of 25 at a time.

Here is two different examples of what I want it to do:

 Product 1, 2, 3, 4 + 5 Prices each = same page
 Product 5 + 6 Prices = this Product with Prices on next page

Or

Product 1 + 23 prices = same page
Product 2 + 20 prices = next page with Product and prices

How can I achieve this?


回答1:


The first thing that comes to my mind is to query over the Price model:

prices = Price.joins(:product).where(:product_id => [1,2,3]).paginate(:page => params[:page])

And to make things easier for your views: prices.map! { |p| p.product }



来源:https://stackoverflow.com/questions/8113402/how-to-paginate-sunspot-in-this-manner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!