如何以这种方式对太阳黑子进行分页?
我有以下模型关联:
class Product < ActiveRecord::Base has_many :prices class Price < ActiveRecord::Base belongs_to :product
这是我的控制器
class SearchController 5, :page => params[:page]) end @products = @search.results 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
我怎样才能做到这一点?
我想到的第一件事就是查询Price模型:
prices = Price.joins(:product).where(:product_id => [1,2,3]).paginate(:page => params[:page])
为了让您的观点更容易: prices.map! { |p| p.product }
prices.map! { |p| p.product }