Rails - meta_search 未正确过滤结果
我有一个产品模型、一个类别模型和一个产品分类模型(has_many...,通过关联)。我正在使用 meta_search gem 进行产品搜索。由于某种原因,即使我的日志提取了正确的标准,它也没有根据类别过滤产品。无论我选择什么,它总是显示所有产品。
产品控制者:
def update
params[:product][:category_ids] ||= []
@product = Product.find(params[:id])
if @product.update_attributes(params[:product])
redirect_to @product
else
render "edit"
end
产品搜索表:
<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>
任何帮助将不胜感激。另外,有没有更好的宝石可供我使用?我知道 searchlogic 不兼容 Rails 3,除非你得到补丁。 searchlogic 的修补版本是否比 meta_search 更好?谢谢。
更新:已解决:
经过几个小时的思考,我想通了。我不确定这就是解释,但是在*从“:product_categorizations_category_id_equals_any”*中删除“any”之后,我让它工作了。我认为原因是因为这是一个通过关联的 has_many,category_id 没有存储为数组,因此“any”不相关。我的解释可能完全不对劲,但它确实有效。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
meta_search 应该可以通过关联为 has_many 正常工作(但也 habtm )例如,我有一个用户有许多团队模型,其中:
以下所有内容都返回正确过滤的结果:
我怀疑您的问题可能不是meta_search 本身,但还有其他一些原因(例如未正确接收表单参数并将其传递给搜索,或者未正确定义关联)。
建议您首先在模型级别验证搜索(在控制台中使用上述查询;但最好在您的项目中对此进行测试)。接下来是确保正确接收参数并将其传递给搜索。
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:
All of the following all return correctly filtered results:
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.