简体   繁体   English

在Rails中使用合并范围进行过滤

[英]Filtering using Scopes in rails

I'm trying to create a records filtering system using ActiveRecord scopes but i am stuck at the point where you have two filters of the same model. 我正在尝试使用ActiveRecord范围创建记录过滤系统,但是我被困在具有两个相同模型过滤器的地方。 I am trying to filter products with brand_id: 1 and brand_id:2 我正在尝试使用brand_id: 1brand_id:2过滤产品

My Product model with the brand scope defined looks like this: 定义了brand范围的“我的Product模型如下所示:

class Product < ActiveRecord::Base
  belongs_to :brand

  scope :brand, lambda{|brand| where('brand_id = ?', brand )}
end

I am trying to filter the products on my categories/show view and the CategoriesController looks like this: 我正在尝试在我的categories/show视图上过滤产品,并且CategoriesController看起来像这样:

class CategoriesController < ApplicationController
  has_scope :brand

  def show
    @category = Category.find(params[:id])
    @products_by_category = apply_scopes(Product).where(category_id: @category).load
    @brands = @category.brands
  end
end

The categories/show view looks like this: categories/show视图如下所示:

<% @products_by_category.each do |product| %>
  <h2><%= product.name %></h2>
<% end %>

i am using this form to do the filtering: 我正在使用此表单进行过滤:

<%= form_tag @category_path, method: get do %>
  <% @brands.each do |brand| %>
    <span class="filter_name"><%= brand.name%>:</span>
    <span> <%= check_box_tag :brand, brand.id, false,
                                     class: 'filter_check_box' %> </span>

   <% end %>
  <%=submit_tag 'filter'%>
<% end %>

The problem is when i select more than one brand in the check_boxes, it filters the products with only the first parameter; 问题是,当我在check_boxes中选择多个品牌时,它仅使用第一个参数过滤产品;

for example if i filter with brands with :id 1 and :id 2 and submits the form, it the request url looks like this: http://localhost:3000/categories/1?&brand=1&brand=4 and the queried array is only filtered with brand=1 . 例如,如果我用:id 1:id 2品牌过滤并提交表单,则它的请求网址如下: http://localhost:3000/categories/1?&brand=1&brand=4 ,查询的数组是仅使用brand=1过滤。 I would like the results to be filtered by both parameters. 我希望通过两个参数过滤结果。 Is the resulting url even correct or should it be: http://localhost:3000/categories/1?&brand=1,4 ? 生成的网址是否正确,还是应该为: http://localhost:3000/categories/1?&brand=1,4

All of the filter inputs you are submitting have the same parameters key, brand . 您要提交的所有过滤器输入都具有相同的参数键brand So, the parameters hash can only have one value for that key. 因此,参数哈希只能对该键具有一个值。

You want to use a check_box_tag instead for 'brand_ids[]' : 您想使用check_box_tag代替'brand_ids[]'

<%= check_box_tag 'brand_ids[]', brand.id, false, class: 'filter_check_box' %>

Finally, and this is important , you cannot send an array to the parameters via a GET action. 最后, 这很重要 ,您不能通过GET操作将数组发送到参数。 You will need to use a POST. 您将需要使用POST。 As the second link below notes, this is a limitation of HTTP, not Rails. 如下面的第二个链接所述,这是HTTP的限制,而不是Rails。

See also: 也可以看看:

Aside 在旁边

I would humbly recommend you refer to variables with more consistent names, for ease of readability and maintenance of code. 我谨建议您使用更一致的名称来引用变量,以便于阅读和维护代码。 If you're using a brand_id, as in that Product scope, call it a brand_id, not a brand. 如果您在该产品范围内使用brand_id,则将其称为brand_id,而不是品牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM