如何以这种方式对黑子进行分页? [英] How to paginate sunspot in this manner?

查看:78
本文介绍了如何以这种方式对黑子进行分页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下模型关联:

class Product < ActiveRecord::Base
  has_many :prices

class Price < ActiveRecord::Base
  belongs_to :product

这是我的控制人

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

然后是我的观点:

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

让我们说我有10种产品,每种产品都有20种不同的价格.我的目标是每页显示5个产品,但是如果一次页面上的价格一次最多超过25个,则价格会转到下一页.

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

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

我该如何实现?

推荐答案

我想到的第一件事是查询价格模型:

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])

并使您的视图更轻松:prices.map! { |p| p.product }

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

这篇关于如何以这种方式对黑子进行分页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆