Rails - meta_search 未正确过滤结果

发布于 2024-12-08 02:16:43 字数 961 浏览 1 评论 0原文

我有一个产品模型、一个类别模型和一个产品分类模型(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你穿错了嫁妆 2024-12-15 02:16:44

meta_search 应该可以通过关联为 has_many 正常工作(但也 habtm )例如,我有一个用户有许多团队模型,其中:

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

以下所有内容都返回正确过滤的结果:

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]

我怀疑您的问题可能不是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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文