В моем интернет-магазине у меня есть таблицы Продукт и Размер , также я думаю, что мне нужно добавить таблицу Пополнение запасов
ВместоЯ думаю, что лучше обновить продукт, лучше иметь таблицу Restocking , тогда я мог бы отслеживать даты добавления новых размеров, количества, а почему бы не новые цены (покупка и продажа) ... и создатьстатистика ...
У тебя это правильно?
Как только Пополнение запасов создано, соответствующий Продукт обновляется с новым количеством и ценой?
Ну,
Итак, все началось так:
#Product
has_many :sizes
accepts_nested_attributes_for :sizes, reject_if: :all_blank, allow_destroy: true
#Size
belongs_to :product
Таблица пополнения запасов должна иметь атрибуты размеров (например, продукт)
Я считаю, что мне нужно использовать полиморфные ассоциации, но как я должен обновлять своисхема, что мне добавить, удалить?
Так как я добавил модель Restocking , мои модели выглядят так:
#Product
has_many :sizes, inverse_of: :product, dependent: :destroy, as: :sizeable
has_many :restockings
accepts_nested_attributes_for :sizes, reject_if: :all_blank, allow_destroy: true
#Restocking
has_many :sizes, as: :sizeable
belongs_to :product
accepts_nested_attributes_for :sizes, reject_if: :all_blank, allow_destroy: true
#Size
belongs_to :product
belongs_to :restocking
belongs_to :sizeable, polymorphic: true, class_name: "Size"
schema.rb
create_table "sizes", force: :cascade do |t|
t.string "size_name"
t.integer "quantity"
t.bigint "product_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "quantity_stock"
t.index ["product_id"], name: "index_sizes_on_product_id"
end
create_table "restockings", force: :cascade do |t|
t.bigint "product_id"
t.bigint "sizeable_id"
t.decimal "price", precision: 10, scale: 2
t.decimal "buying_price", precision: 10, scale: 2
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["product_id"], name: "index_restockings_on_product_id"
t.index ["sizeable_id"], name: "index_restockings_on_sizeable_id"
end
create_table "products", force: :cascade do |t|
t.string "title", limit: 150, null: false
t.text "description"
t.bigint "category_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "color"
t.integer "user_id"
t.json "attachments"
t.string "brand"
t.string "ref"
t.decimal "price"
t.decimal "buying_price", precision: 10, scale: 2
t.index ["category_id"], name: "index_products_on_category_id"
end
На данный моментУ меня есть несколько ошибок, например
в ProductsController
def new
@product = Product.new
@product.sizes.build
end
ошибка:
ActiveModel::UnknownAttributeError at /admin/products/new
unknown attribute 'sizeable_id' for Size.
Можете ли вы рассказать мне о миграциях, которые я должен изменить?Предложения приветствуются