i've following model setup

class Category < ActiveRecord::Base

  has_ancestry :cache_depth => true, :depth_cache_column => :depth

  has_many :watches, :dependent => :destroy
  has_many :products, :through => :watches
end

class Watch < ActiveRecord::Base

  belongs_to :category
  has_many :products
end

class Product < ActiveRecord::Base

  belongs_to :watch, :counter_cache => true
  belongs_to :category
end

I have to find items through groups title. Category have 2 levels deep(tree structure). 1 - level is really a make, 2 - serie. For the time being im build this kind of search query with the aid of meta_search jewel

@products = (Product.search :watch_category_name_contains => params[:search]).all.paginate(:page => params[:page])

This works and return all items with serie_title. But watch table always contain only category_id of two level category(serie), and im have to have the ability to search items through makes(1 level category). How do i build this kind of query? Thanks!

Well, i see some upvotes developing my old question, and so i will answer. Im finish with raw sql for makes and series queries. Here you go:

  def self.makes_with_products
    find_by_sql "
SELECT makes.* FROM categories makes
WHERE ancestry IS NULL AND makes.id IN (
    SELECT series.ancestry FROM products p
    INNER JOIN watches w ON w.id = p.watch_id
    INNER JOIN categories series ON series.id = w.category_id
    WHERE series.ancestry = makes.id AND p.active
  )
"
  end

  def series_with_products
   find_by_sql "
SELECT series.* FROM categories series
WHERE series.ancestry = '#{id}'
  AND (
    SELECT COUNT(*) FROM products p
    INNER JOIN watches w ON w.id = p.watch_id
    WHERE w.category_id = series.id AND p.active
  ) > 0
"
  end

Hope this help someone.