我有许多产品,这些产品与使用会员资格的 has_many 相关联。
我正在尝试创建一个搜索框,任何人都可以在其中搜索产品,同时还可以使用类别下拉列表过滤他们的搜索(因此只能检索具有相关类别的产品)。
Thinking_sphinx 索引在产品模型中我没有收到任何错误,但下拉菜单不会影响搜索。
MODEL:
has_many :memberships,:dependent=> :destroy
has_many :categories, :through => :memberships
named_scope :published, :conditions => {:publish => 1}
define_index do
indexes product_name
indexes product_description
indexes publish
indexes memberships.product_id
indexes memberships.category_id
indexes categories.category_name
end
end
CONTROLLER:
@products = Product.search params[:search],:conditions=>{@product.memberships.category_id =>params[:category_product] },:page=> params[:page] || 1,:per_page =>4
VIEW:
form_tag search_path, :method =>:get do
text_field_tag :search, params[:search]
form_tag categories_path, :method => :get do
select_tag"category", options_from_collection_for_select (Category.find (:all, :group=>:id), :id, :category_name,params[:category_product])
end
submit_tag "search", :name => nil
end