class Category < ActiveRecord::Base
has_many :products
acts_as_nested_set :order => "name"

def children_products
    ([self.products] + children.collect(&:children_products)).flatten
end
end

class Product < ActiveRecord::Base
belongs_to :category
end

In the example (p is a shortcut for product)
category1
--category2 (p1, p2, p3)
----category3 (p4, p5)
----category4 (p6)

category2.children_products will result in products 1-6. this solution
works but its very inefficient.

the nested set plugin added the lft, rgt and parent_id columns to the
category model.

On 11 Lip, 11:41, Pat Allan <[email protected]> wrote:
> Hi Sebastian
>
> Can you give us some idea of the schema of these two models - particularly, 
> how they relate to each other (and themselves, in the case of recursion)?
>
> --
> Pat
>
> On 11/07/2011, at 7:06 PM, Sebastian Wojtczak wrote:
>
>
>
>
>
>
>
> > Hi,
> > is it possible to use thinking sphinx with nested sets and do a
> > recursive search?
> > I have two models: categories and products.
>
> > category1
> > --category2
> > ----category3
> > ----category4
>
> > after choosing category2 I want all products from categories 2, 3 and
> > 4. This is easily accomplished with activerecord or meta_search gem
> > but with the amount of data it has to search or even flipping pages
> > (will_paginate gem) its taking too much time.
>
> > --
> > You received this message because you are subscribed to the Google Groups 
> > "Thinking Sphinx" 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 
> > athttp://groups.google.com/group/thinking-sphinx?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"Thinking Sphinx" 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/thinking-sphinx?hl=en.

Reply via email to