Итак, я пытаюсь создать запись, связанную с двумя таблицами, но не могу заставить ее работать.
Параметры: {"name" => "asdfasd", "upc" => "243252353", "availableOn" => "07/12/2020", "properties" => [{"name" => "material", "value" => "jean"}], "send_datum" => {"name" => "asdfasd", "upc" => "243252353", "availableOn" => "12.07.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 (значение: params [: свойства] [x] [: значение])
Этомоя первая реакция на проект рельсов и понимание таблицы ассоциаций.было настоящим испытанием, но я добираюсь туда.
Модели:
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 от продукта.Как мне это исправить?
Любая помощь с благодарностью.Спасибо !!