首页 > 解决方案 > 创建属于 2 个单独表的记录

问题描述

所以我试图创建一个与 2 个表相关联的记录,但我似乎无法让它工作。

参数:{"name"=>"asdfasd", "upc"=>"243252353", "availableOn"=>"07/12/2020", "properties"=>[{"name"=>"material", "value"=>"jean"}], "send_datum"=>{"name"=>"asdfasd", "upc"=>"243252353", "availableOn"=>"07/12/2020", "properties "=>[{"name"=>"material", "value"=>"jean"}]}}

class SendDataController < ApplicationController
    protect_from_forgery with: :null_session

    def save
        product = Product.create(name:params[:name], upc:params[:upc].to_i, available_on:params[:availableon])

        x=0
        while x < params[:properties].length
            property = product.properties.create(name:params[:properties][x][:name])
            property.product_properties.create(value:params[:properties][x][:value])

            x += 1;
        end

    end    
end

这条线是我无法接缝上班的那条线:

property.product_properties.create(value:params[:properties][x][:value])

这是我对 rails 项目和了解 table assoc 的第一次反应。一直是一个真正的挑战,但我正在到达那里。

楷模:

class Property < ApplicationRecord
  belongs_to :product
  has_many :product_properties
end

class Product < ApplicationRecord
  has_many :properties
  has_many :product_properties
end

class ProductProperty < ApplicationRecord
  belongs_to :property
  belongs_to :product
end

移民:

class CreateProducts < ActiveRecord::Migration[5.2]
  def change
    create_table :products do |t|
      t.string :name
      t.string :upc
      t.datetime :available_on

      t.timestamps
    end
  end
end

class CreateProperties < ActiveRecord::Migration[5.2]
  def change
    create_table :properties do |t|
      t.string :name

      t.timestamps
    end
  end
end

class CreateProductProperties < ActiveRecord::Migration[5.2]
  def change
    create_table :product_properties do |t|
      t.string :value

      t.timestamps
    end
  end
end

class AddProductRefToProperties < ActiveRecord::Migration[5.2]
  def change
    add_reference :properties, :product, foreign_key: true
  end
end

class AddProductRefToProductProperties < ActiveRecord::Migration[5.2]
  def change
    add_reference :product_properties, :product, foreign_key: true
  end
end

class AddPropertiesRefToProductProperties < ActiveRecord::Migration[5.2]
  def change
    add_reference :product_properties, :property, foreign_key: true
  end
end

架构:

ActiveRecord::Schema.define(version: 2018_09_24_163027) do

  create_table "product_properties", force: :cascade do |t|
    t.string "value"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "product_id"
    t.integer "property_id"
    t.index ["product_id"], name: "index_product_properties_on_product_id"
    t.index ["property_id"], name: "index_product_properties_on_property_id"
  end

  create_table "products", force: :cascade do |t|
    t.string "name"
    t.string "upc"
    t.datetime "available_on"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "properties", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "product_id"
    t.index ["product_id"], name: "index_properties_on_product_id"
  end

end

更新::
这与 product_property 从属性中获取 property_id 但没有从产品中获取 product_id 的事实有关。我该如何解决?

任何帮助是极大的赞赏。谢谢!!

标签: ruby-on-rails

解决方案


问题是我的关联设置不正确。正如@DavidAldridge 指出的那样,我需要通过产品属性将属性链接到产品。

class Product < ApplicationRecord
  has_many :product_properties
  has_many :properties, through: :product_properties
end

class ProductProperty < ApplicationRecord
  belongs_to :property, required: false
  belongs_to :product, required: false
end

class Property < ApplicationRecord
  has_many :product_properties
  has_many :products, through: :product_properties
  accepts_nested_attributes_for :product_properties
end

推荐阅读