views:

19

answers:

2

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.

A: 

This should work:

@category.products.find(:all, :limit => 8)
Can Berk Güder
Hi Can, thanks for the reply, but I am trying to get to the deals associated with the products, that's wha I do the collect.
val_to_many
@val_to_many: you can add `.collect` to this, I'm updating my answer.
Can Berk Güder
wait a second, do you want 8 deals or 8 products?
Can Berk Güder
A: 

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)
j.
Outch! That's a lot simpler than the knot in my brain had figured ... Thanks a lot! Val
val_to_many
You're welcome :]
j.