【发布时间】:2011-10-12 10:34:10
【问题描述】:
我有以下模型设置
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
我需要通过类别名称查找产品。类别有 2 级深度(树结构)。 1 - 级别是品牌,2 - serie。现在我在meta_search gem 的帮助下构建这种类型的搜索查询
@products = (Product.search :watch_category_name_contains => params[:search]).all.paginate(:page => params[:page])
这有效并返回所有带有 serie_name 的产品。但是手表表总是只包含2级类别(系列)的category_id,我需要能够通过品牌(1级类别)搜索产品。我怎样才能建立这种类型的查询?谢谢!
【问题讨论】:
标签: ruby-on-rails database search activerecord