三个模型,category
, tag
, product
,
模型的关系
#category
class Category < ActiveRecord::Base
has_many tags
end
#tag
class Tag < AcriveRecord::Base
has_many :products
belongs_to category
end
#product
class Product < AcriveRecord::Base
belongs_to tag
end
现在的需求是在category
对products
进行分页
#categories/show
#
#我现在是这么做的, 但是没什么分页效果,
def show
@category = Category.find(params[:id])
@tag = @category.tags.build
@products = @tag.products.paginate(page: params[:page],per_page: 10)
end
#category/view
<div class="cate-show">
<% @category.tags.each do |tag| %>
<% tag.products.each do |product| %>
<ul class="thumbnail">
<li> <%= image_tag(product.image_url) %></li>
</ul>
<% end %>
<% end %>
</div>
<%= will_paginate @products %>
这样没有分页效果,如果对@tags = @category.tags.paginate(page: params[:page],per_page: 2)
只对tag
进行分页,没有对products
进行分页