Ruby on rails 如何以这种方式对太阳黑子进行分页?
我有以下模型关联:Ruby on rails 如何以这种方式对太阳黑子进行分页?,ruby-on-rails,ruby,ruby-on-rails-3,pagination,will-paginate,Ruby On Rails,Ruby,Ruby On Rails 3,Pagination,Will Paginate,我有以下模型关联: class Product < ActiveRecord::Base has_many :prices class Price < ActiveRecord::Base belongs_to :product 或 如何实现这一点?我想到的第一件事是询问价格模型: prices = Price.joins(:product).where(:product_id => [1,2,3]).paginate(:page => params[:pag
class Product < ActiveRecord::Base
has_many :prices
class Price < ActiveRecord::Base
belongs_to :product
或
如何实现这一点?我想到的第一件事是询问价格模型:
prices = Price.joins(:product).where(:product_id => [1,2,3]).paginate(:page => params[:page])
为了让您的视图更简单:
prices.map!{| p | p.product}
老实说,我很难跟上。首先-一个产品如何一方面“有许多价格”,另一方面“属于许多价格(复数!)”?@polarblau我通过提问进行编辑,以提供更清晰的信息。对不起,这很难完全理解。我是在搜索价格
还是在搜索产品
呢?我发布的代码是在搜索价格。它所做的是将表Price与表Product进行内部联接,以获取与一组特定产品id匹配的所有价格。然后,对结果进行分页。最后,最后一行代码反转结果集,按产品对其进行索引。我从未使用过Sunspot,但我想您必须将搜索分为两部分:1)@search=Product search do…
不分页;2) 我在上面的帖子中描述的方法是将太阳黑子搜索的结果集作为产品id输入。
<%= will_paginate @products %>
<% @products.each do |product| %>
<%= product.name %>
<% prices.each do |price| %>
<%= price.price %>
<% end %>
<% end %>
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
prices = Price.joins(:product).where(:product_id => [1,2,3]).paginate(:page => params[:page])