:限制在收集中找到的行数(has_many 关联)
类别
has_many :products
has_many :deals, :through => :products
产品
has_many :deals
我想在类别页面上显示有限数量的交易。
在categories_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) 进行第二次查找,我只会根据要求显示它。
Category
has_many :products
has_many :deals, :through => :products
Product
has_many :deals
I want to display a limited number of deals on a category page.
In categories_helper.rb:
def deals
@category.products.collect { |c| c.deals}.flatten
end
In the show.html.erb (Category):
<% for deal in deals %>
<%= deal.name %>
<% end %>
This works fine, BUT it obviously throws out all deals for the products in that category and I want only 8 of them. So I would like to apply a (:limit => 8) to the .collect. I just can't figure out where it would go. Also I would like to do a second find with an (:offset => 8) which I'll show only on request.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该有效:
This should work:
您不需要
collect
,因为您具有has-many-through
关联。我相信这就是您正在寻找的:You don't need the
collect
since you have thehas-many-through
association. I believe this is what you're looking for: