【问题标题】:Create nested hash of an array of hashes over a Rails ActiveRecord Query在 Rails ActiveRecord 查询上创建哈希数组的嵌套哈希
【发布时间】:2022-11-12 02:06:50
【问题描述】:

我正在使用 rails 7,我需要创建这个数据结构。

给定产品的可用选项组合的“树”结构。

{
  "option": "color",
  "values": {
    "red": {
      "option": "size",
      "values": {
        "S": {
          "option": "material",
          "values": {
            "cotton": {
              "sku": "shirt-red-s-cotton"
            }
          },
        },
        "M": { ... },
        "L": { ... },
      }
    },
    "green": { ... },
    "blue": { ... }
  }
}

我有下一个架构:


ActiveRecord::Schema[7.0].define(version: 2022_11_04_214231) do
  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "items", force: :cascade do |t|
    t.bigint "product_id", null: false
    t.json "p_options"
    t.string "sku"
    t.integer "stock"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["product_id"], name: "index_items_on_product_id"
    t.index ["sku"], name: "index_items_on_sku", unique: true
  end

  create_table "product_option_lists", force: :cascade do |t|
    t.bigint "product_id", null: false
    t.bigint "product_option_id", null: false
    t.string "option"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["product_id"], name: "index_product_option_lists_on_product_id"
    t.index ["product_option_id"], name: "index_product_option_lists_on_product_option_id"
  end

  create_table "product_options", force: :cascade do |t|
    t.string "option"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["option"], name: "index_product_options_on_option", unique: true
  end

  create_table "products", force: :cascade do |t|
    t.string "name"
    t.boolean "active"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["name"], name: "index_products_on_name", unique: true
  end

  add_foreign_key "items", "products"
  add_foreign_key "product_option_lists", "product_options"
  add_foreign_key "product_option_lists", "products"
end

我相信开始制作这个数据结构所需的所有信息都可以通过 ActiveRecord 使用:

Item.where(product_id: <some_id>)


# or maybe mapping it to get only the options of the product


Item.where(product_id: <some_id>).map{ |item| item.p_options }

第二个查询返回如下值:(根据我使用的种子数据)

[{ "Size" => "S", "Color"=>"Red"   , "Material"=>"Cotton"},
 { "Size" => "S", "Color"=>"Green" , "Material"=>"Silk"},
                     -----------
 { "Size" => "XL", "Color" => "Blue", "Material"=> "Cotton"}]

我还认为这可以通过递归每个键的可能值来完成。但我仍然没有掌握 Hashes 构造的递归。


也许我已经制作的这个端点很有用。它返回给定产品的此数据结构。

[
  { "option": "color", "values": ["Red","Green", "Blue"]},
  { "option": "size", "values": ["S", "M", "L"]},
  ...
]

【问题讨论】:

    标签: ruby-on-rails ruby data-structures


    【解决方案1】:

    这是我用递归创建的一个演示

    create_table "product_options", force: :cascade do |t|
     t.string "option_label"
     t.string "option_text"
     t.string "value_label"
     t.bigint "product_id", null: false
     t.datetime "created_at", null: false
     t.datetime "updated_at", null: false
     t.bigint "parent_id"
     t.index ["parent_id"], name: "index_product_options_on_parent_id"
    end
    
    create_table "products", force: :cascade do |t|
     t.string "name"
     t.boolean "active"
     t.datetime "created_at", null: false
     t.datetime "updated_at", null: false
     t.index ["name"], name: "index_products_on_name", unique: true
    end
    
    add_foreign_key "product_options", "product_options", column: "parent_id"
    add_foreign_key "product_options", "products"
    
    
    class ProductOption < ApplicationRecord
    

    属于_to:父级,可选:true,class_name:'ProductOption' has_many:options,class_name:'ProductOption',foreign_key::parent_id,依赖::destroy
    结尾

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-03
      • 1970-01-01
      • 2015-06-22
      • 1970-01-01
      • 2020-07-02
      • 1970-01-01
      相关资源
      最近更新 更多