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? Greg -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 13 July 2010 09:26, Greg Ma <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law wrote:> On 13 July 2010 09:26, Greg Ma <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> � � �:joins => "inner join categories_products on (categories.id >> > 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. > > ColinYes I know that but I''m not trying to get Products but Categories with products -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 13 July 2010 10:36, Greg Ma <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Colin Law wrote: >> On 13 July 2010 09:26, Greg Ma <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >>> � � �:joins => "inner join categories_products on (categories.id >>> >> 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 > > Yes I know that but I''m not trying to get Products but Categories with > productsI 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 -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law wrote:> On 13 July 2010 10:36, Greg Ma <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >>> Colin >> >> 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? > > ColinThat 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. -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Greg Ma wrote:> Colin Law wrote: >> On 13 July 2010 10:36, Greg Ma <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >>>> Colin >>> >>> 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.named_scope :with_products, lambda { { :select => "distinct categories.id", :joins => "inner join categories_products on (categories.id = categories_products.category_id)" }} It should be something like that, but this returns the categories id who has products, and I want the ActiveRecord object not the ids... -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 13 July 2010 13:03, Greg Ma <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Colin Law wrote: >> On 13 July 2010 10:36, Greg Ma <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >>>> Colin >>> >>> 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 } 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.