如何以这种方式对太阳黑子进行分页?

发布于 2024-12-15 09:41:12 字数 1107 浏览 2 评论 0原文

我有以下模型关联:

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 个,则价格会转到下一页。

这是我想要它做什么的两个不同的例子:

 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

我怎样才能实现这个?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔戏命师 2024-12-22 09:41:12

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

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

并且为了让您的视图更容易:prices.map! { |p| p.产品}

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 }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文