开发者

rails - meta_search not filtering results properly

开发者 https://www.devze.com 2023-04-11 01:29 出处:网络
I have a product model, a category model, and a product_categorization model (has_many..., through association).I am using the meta_search gem to conduct product searches.For some reason, even though

I have a product model, a category model, and a product_categorization model (has_many..., through association). I am using the meta_search gem to conduct product searches. For some reason, even though my log is pulling the correct criterias, it is not filtering the products based on the category. No matter what I choose, it always displays all the products.

Product controller:

def update
params[:product][:category_ids] ||= []
@product = Product.find(params[:id])
if @product.update_attributes(params[:product])
 redirect_to @product
else
 render "edit"
end 

product search form:

  <div class="field">
   <%= f.label :category %>
   <%= f.collection_select :product_categorizations_category_id_equals_any, Category.all, :id, :name, :include_blank => true, :prompt => "Select a category" %> 
  </div>   

Any help would be appreciated. Also, is there a better gem out there that I can use? I know searchlogic is not rails 3 compatible unless you get the patch. Is the patched version of searchlogic a better option than meta_search? Thanks.

UPDATE: RESOLVED:

Sooo after many hours of mulling this over, I figured it out. I am not sure this is the开发者_开发知识库 explanation, but after *deleting the "any" from ":product_categorizations_category_id_equals_any"*, I got it to work. I think the reason is because this is a has_many through association, the category_id is not stored as an array so the "any" was not relevant. My explanation may be completely off, but it works.


meta_search should be working fine for has_many through associations (but also habtm) e.g. I have a user has many teams model where:

class User < ActiveRecord::Base
  has_many :users_teams, :class_name => 'UsersTeams', :dependent => :destroy
  has_many :teams, :through => :users_teams
end

All of the following all return correctly filtered results:

User.search :teams_id_equals=>999
User.search :users_teams_team_id_equals=>999
User.search :teams_id_equals_any=>[999]
User.search :users_teams_team_id_equals_any=>[999]

I suspect your problem may not be meta_search itself, but some other cause (like the form parameters not being correctly received and passed to search, or the associations not defined correctly).

Suggest that you first verify the search at the model level (either in the console with queries like the ones above; but ideally have a test in your project for this). Next would be to ensure that the parameters are being correctly received and passed to search.

0

精彩评论

暂无评论...
验证码 换一张
取 消