:限制在收集中找到的行数(has_many 关联)

发布于 2024-09-09 22:21:27 字数 651 浏览 4 评论 0原文

类别

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぇ气 2024-09-16 22:21:28

这应该有效:

@category.products.find(:all, :limit => 8)

This should work:

@category.products.find(:all, :limit => 8)
人心善变 2024-09-16 22:21:27

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

@category.deals.all(:limit => 8)

You don't need the collect since you have the has-many-through association. I believe this is what you're looking for:

@category.deals.all(:limit => 8)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文