On 13 July 2010 09:26, Greg Ma <[email protected]> wrote:
> Hi,
>
> I have a Category and a Model product with an association table
> categories_products.
>
> I want a named that returns the categories with products.
> Here is what I've done (in the Category model):
>
> named_scope :with_products, lambda {
>    {
>      :joins => "inner join categories_products on (categories.id =
> categories_products.category_id)"
>
>    }}
>
>
> The problem with this named_scopes is that it returns X number of the
> time the same category. (X is the number of products that the category
> as)
>
> What in my named scope I have to add to avoid this?
>

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.

Colin

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to