简体   繁体   English

如何使用很少的选项从连接表中选择数据记录?

[英]How to select data records from join table with few options?

I'm writing a simple online shop on ruby and have a problem with items filtering.我正在用 ruby​​ 编写一个简单的在线商店,但在项目过滤方面遇到了问题。 I'm trying to filter items by few options (eg 'color': 'red' and 'brand': 'zara').我正在尝试通过几个选项过滤项目(例如“颜色”:“红色”和“品牌”:“zara”)。

I expect to see only items that have both 'red' and 'zara' options.我希望只看到同时具有“红色”和“zara”选项的项目。

Everything is OK when I filter by one option.当我按一个选项过滤时,一切正常。 But with few options, it shows all items that have at least one of the options.但由于选项很少,它会显示至少具有其中一个选项的所有项目。

My filtering scope in Item model:我在项目模型中的过滤范围:

scope :by_options,
    ->(options) { joins(:items_options).where(items_options: { option_id: options }).distinct }

Items controller:物品控制器:

class ItemsController < ApplicationController
  def index
    @items = Item.all

    filtering_params(params).each do |key, value|
      @items = @items.public_send("by_#{key}", value) if value.present?
    end
  end

  private
    def filtering_params(params)
      params.slice(:category, :options)
    end
  end

Generated query:生成的查询:

SELECT DISTINCT "items".* FROM "items" INNER JOIN "items_options" ON "items_options"."item_id" = "items"."id" WHERE "items_options"."option_id" IN (?, ?)  [["option_id", 1], ["option_id", 6]]

I also tried to filter by options twice, but in this case @items are empty (6 and 1 are just examples, because I have item that have both of this options:我还尝试按选项过滤两次,但在这种情况下,@items 为空(6 和 1 只是示例,因为我的项目同时具有这两个选项:

@items = Item.by_options(6).by_options(1)

Generated query:生成的查询:

SELECT DISTINCT "items".* FROM "items" INNER JOIN "items_options" ON "items_options"."item_id" = "items"."id" WHERE "items_options"."option_id" = ? AND "items_options"."option_id" = ?  [["option_id", 6], ["option_id", 1]]

DB schema:数据库架构:

create_table "items", force: :cascade do |t|
  t.string "name", null: false
  t.decimal "price", null: false
  t.integer "available_count", null: false
  t.integer "category_id"
  t.datetime "created_at", precision: 6, null: false
  t.datetime "updated_at", precision: 6, null: false
  t.index ["category_id"], name: "index_items_on_category_id"
end

create_table "items_options", id: false, force: :cascade do |t|
  t.integer "item_id", null: false
  t.integer "option_id", null: false
end

create_table "options", force: :cascade do |t|
  t.string "name", null: false
  t.integer "filter_id"
  t.datetime "created_at", precision: 6, null: false
  t.datetime "updated_at", precision: 6, null: false
  t.index ["filter_id"], name: "index_options_on_filter_id"
end

You can try:你可以试试:

scope :by_options,
    ->(options) { 
          self.joins(:items_options)
              .having('SUM(items_options.option_id in (?)) = ?', options, options.size)
     }

You're basically saying: "Give me all items that have all the items_options.option_id I specified".您基本上是在说:“给我所有具有我指定的所有 items_options.option_id 的项目”。

This is a little bit dirty, and I haven't tested it, but from the top of my head it should do the trick.这有点脏,我还没有测试过,但从我的头顶来看,它应该可以解决问题。

Otherwise, would you mind linking an SQL schema creation statement to your post so we could run the queries more easily?否则,您介意将 SQL 模式创建语句链接到您的帖子,以便我们可以更轻松地运行查询吗?

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

相关问题 在Ruby on Rails的多对多关系中,如何使用父表形式的数据在连接表中创建记录? - In Many to Many relationships in Ruby on Rails, how to create records in join table with data from a parent table form? 如何从表中选择6条随机记录 - How to select 6 random records from table Rails:在联接表上选择相关的活动记录 - Rails: select related active records on join table 如何将两个记录连接起来并返回表数据? - How do i join the two records and return the table data? select_tag选项应该从另一个表中获取数据 - select_tag options should fetch data from another table 如何在Rails 5中从具有匹配ID的联接表中获取记录数 - How to get count of records from join table with matching ID in Rails 5 如何从一个表中选择所有记录,并且如果它们的ID在另一个表中,那么又如何从表2中获取数据? - How can I select all records from one table and if their id is present in another then get data from table 2 also? 如何在 Rails 中选择通过连接到具有多个记录的另一个表的最新记录 - How to select the most recent record in Rails that's via a join to another table with multiple records 如何有条件地联接并从联接表中获取数据? - How to join conditionally and get data from the join table? 如何在rails中添加options_from_collection_for_select的数据属性 - How to add data attribute for options_from_collection_for_select in rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM