太阳黑子:分页一定量的关联?

发布于 2024-12-15 16:58:49 字数 789 浏览 3 评论 0原文

如果我有以下关联:

Product has_many :user_prices
UserPrice belongs_to :product

我的目标是每页显示 5 个产品,并且只显示这么多的 user_prices(假设每个 5 个):

class SearchController < ApplicationController

  def index
    @search = Product.search do
      fulltext params[:search]
      # This gives 5 producuts per page but how about that and 5 UserPrices?
      paginate(:per_page => 5, :page => params[:page])
    end
    @products = @search.results
  end
end

视图:搜索/索引

<% @products.each do |p| %>
 <%= p.name %>
  <% p.user_prices.each do |up| %>
    <%= number_to_currency(up.price) %>
    <%= up.purchase_date %>
  <% end %>
<% end %>

您如何能够在太阳黑子?

If I have the following associations:

Product has_many :user_prices
UserPrice belongs_to :product

And my goal is to show 5 Products per page and only so many user_prices ( lets say 5 each):

class SearchController < ApplicationController

  def index
    @search = Product.search do
      fulltext params[:search]
      # This gives 5 producuts per page but how about that and 5 UserPrices?
      paginate(:per_page => 5, :page => params[:page])
    end
    @products = @search.results
  end
end

View: search/index

<% @products.each do |p| %>
 <%= p.name %>
  <% p.user_prices.each do |up| %>
    <%= number_to_currency(up.price) %>
    <%= up.purchase_date %>
  <% end %>
<% end %>

How would you be able to do this in Sunspot?

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

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

发布评论

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

评论(1

岁吢 2024-12-22 16:58:49

Will_Paginate 在这里不会为您提供帮助,因为您已经对 Product 模型的集合进行了“分页”。我认为你最好按照你的观点做这样的事情:

<% @products.each do |p| %>
  <% p.user_prices.last(5).each do |price| %>
    <%= #some info from user prices %>
  <% end %>
<% end %>

这只是举例。您还可以为 UserPrice 模型实现一些范围,比如订购它或其他什么。
希望它能帮助你。祝你好运 :)

Will_Paginate will not help you here, because you already 'paginated' collection of Product model. I think it will better for you to do something like this into your view:

<% @products.each do |p| %>
  <% p.user_prices.last(5).each do |price| %>
    <%= #some info from user prices %>
  <% end %>
<% end %>

this is just for example. You can also implement some scope for UserPrice model, say to order it or whatever.
Hope it will help you. Good luck :)

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