创建属于2个独立表的记录

时间:2018-09-24 19:59:59

标签: ruby-on-rails

因此,我试图创建与2个表相关联的记录,但似乎无法使其正常工作。

  

参数:{“ name” =>“ asdfasd”,“ upc” =>“ 243252353”,“ availableOn” =>“ 07/12/2020”,“ properties” => [{“ name” =>“ material”,“ value” =>“ jean”}],“ send_datum” => {“ name” =>“ asdfasd”,“ upc” =>“ 243252353”,“ availableOn” =>“ 2020/12/17” ,“属性” => [{“名称” =>“材料”,“值” =>“牛仔裤”}]}}

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项目和了解表asoc的第一个反应。一直是一个真正的挑战,但我要到达那里。

型号:

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。我该如何解决?

任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

问题是我的关联设置不正确。正如@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