Assuming that you have defined has_and_belongs_to relationships
between the classes (or has_many through if that is what you prefer)
then if you have a category you can use @category.products to give you
and array of products for that category, there is no need for a named
scope. Such is the magic of Rails.
I thought you meant "categories (along) with (their) products".
Do you mean that you want all the categories that have at least one
product, ie where category.products.count > 0?
Yes I know that but I'm not trying to get Products but Categories with
products
I thought you meant "categories (along) with (their) products".
Do you mean that you want all the categories that have at least one
product, ie where category.products.count > 0?
Colin
That doesnt work either, that tells you if the current category has more
than one products.
And I want all the categories which has at least one product.
I did not say that is the solution, I asked whether you were trying to
find all categories that meet that condition, to which I think the
answer is yes.
I suggest getting it working in a find first, then convert it to a
named scope. You could use something like
Category.find( :all, :include => :products ).select { |c| c.products.count > 0 }