太阳黑子:分页一定量的关联?
如果我有以下关联:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Will_Paginate 在这里不会为您提供帮助,因为您已经对
Product
模型的集合进行了“分页”。我认为你最好按照你的观点做这样的事情:这只是举例。您还可以为 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: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 :)