简体   繁体   English

如何通过“和”(而不是“或”)在数据库中查询参数

[英]How can I query my db for params by “and” instead of “or”

I have habtm relation between products and colors. 我对产品和颜色之间有牵连。 When I preform a query on products that are "red" and "black" I want it to return product that have "red" AND "black" associations not "red" OR "black" 当我对“红色”和“黑色”产品进行查询时,我希望它返回具有“红色”和“黑色”关联而不是“红色”或“黑色”的产品

This is my scope for this query: 这是我对此查询的范围:

scope :items_design_filter_color, -> (colors) { joins(:colors).where('colors.id' => colors.to_i) unless colors.nil? }

colors params 颜色参数

params[:colors] = ["1", "2"]

colors table is just id and name columns 颜色表只是id和name列

Calling my scopes: 调用我的范围:

@products = @产品=

Kaminari.paginate_array(ItemsDesign.items_design_by_category(@category.subtree_ids)
                                        .items_design_filter_color(params[:colors])
                                        .items_design_filter_sort(sort)
                                        .items_design_filter_editors_pick(params[:editors_pick])
                                        .items_design_filter_sold_out(params[:sold_out])
                                        .items_design_filter_style(params[:style])
                                        .items_design_filter_store(params[:store])
                                        .items_design_filter_price(params[:low_end], params[:high_end]))
    .page(params[:page]).per(48)

this is my attempt at using one of the answers bellows method within my scope: 这是我尝试在我的范围内使用以下波纹管方法之一:

scope :items_design_by_category, -> (category) { joins(:items_categories).where('items_categories.id' => category) }
  scope :items_design_filter_color, -> (colors) { joins(:colors).where(colors: {id: colors}).each.select do |item|
                                                    (item.colors.map(&:id) & colors).size == colors.size
                                                  end unless colors.nil? }
  scope :items_design_filter_style, -> (styles) { where('items_style_id' => styles) unless styles.nil? }
  scope :items_design_filter_store, -> (stores) { where('store_id' => stores) unless stores.nil? }
  scope :items_design_filter_editors_pick, -> (editors_pick) { where('editors_pick' => TRUE) unless editors_pick.nil? }
  scope :items_design_filter_sold_out, -> (sold_out) { where('sold_out' => 'n') unless sold_out.nil? }
  scope :items_design_filter_price, -> (lowend, highend) { where('price_as_decimal' => lowend..highend) unless lowend.nil? }
  scope :items_design_filter_sort, -> (sort) { order(sort) unless sort.nil? }

OK, well I ended up getting this way based on the someones answer that was deleted. 好的,我最终根据被删除的某人的答案获得了这种方法。 If they want to come and put the answer back I will give them credit. 如果他们想回来回答,我会给他们功劳。 In the meantime this solved my problem. 同时,这解决了我的问题。

scope :items_design_filter_color, -> (colors) { color_ids = colors.map(&:to_i) unless colors.nil?
                                                includes(:colors).where(colors: {id: color_ids}).select do |item|
                                                  (item.colors.map(&:id) & color_ids).size == color_ids.size
                                                end unless colors.nil? }

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

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