1

类别

has_many :products
has_many :deals, :through => :products

产品

has_many :deals

我想在类别页面上显示有限数量的交易。

在 category_helper.rb 中:

def deals
 @category.products.collect { |c| c.deals}.flatten
end

在 show.html.erb(类别)中:

<% for deal in deals  %>
 <%=  deal.name %>
<% end %>

这很好用,但它显然会抛出该类别产品的所有交易,我只想要其中的 8 个。所以我想将 (:limit => 8) 应用于.collect。我只是不知道它会去哪里。此外,我想使用 (:offset => 8) 进行第二次查找,仅应要求显示。

4

2 回答 2

2

你不需要,collect因为你有has-many-through协会。我相信这就是您正在寻找的:

@category.deals.all(:limit => 8)
于 2010-07-14T12:59:16.580 回答
1

这应该有效:

@category.products.find(:all, :limit => 8)
于 2010-07-14T12:38:38.643 回答