# Terminal bin/rails g mannequin product identify class sku in_stock:boolean
# db/migrate/20220501015917_create_products.rb class CreateProducts < ActiveRecord::Migration[7.0] def change create_table :merchandise do |t| t.string :identify t.string :class t.string :sku, index: { distinctive: true } t.boolean :in_stock, default: true t.timestamps finish finish finish
# fashions/product.rb class Product < ApplicationRecord attr_readonly :sku finish
# Rails Console product = Product.first product.sku = "changedsku" product.save # document updates, however sku attribute is ignored
# Rails Console merchandise = [ { name: 'Apple 14" Macbook Pro (2021)', category: "Laptop", sku: "MKGR3LL", in_stock: true }, { name: 'Apple 16" Macbook Pro (2021)', category: "Laptop", sku: "MK1E3LL", in_stock: true }, { name: 'Apple Mac Mini (2020)', category: "Desktop", sku: "MGNR3LL", in_stock: true }, { name: 'Apple Mac Studio (2022)', category: "Desktop", sku: "MJMW3LL", in_stock: true } ] Product.insert_all(merchandise) laptops = [ { name: 'Apple 14" Macbook Pro (2021)', sku: "MKGR3LL", in_stock: true }, { name: 'Apple 16" Macbook Pro (2021)', sku: "MK1E3LL", in_stock: true } ] Product.create_with(class: "Laptop computer").insert_all(laptops)
# Rails Console desktops = [ { name: 'Apple Mac Mini (2020)', category: "Desktop", sku: "MGNR3LL", in_stock: false }, { name: 'Apple Mac Studio (2022)', category: "Desktop", sku: "MJMW3LL", in_stock: true } ] Product.upsert_all(desktops, update_only: [:in_stock], unique_by: [:sku])
# Rails Console product = Product.first product.toggle(:in_stock) product.save product.toggle!(:in_stock)
# Terminal rails g mannequin automobile identify kind
# fashions/automotive.rb class Automotive < Car WHEELS = 4 finish
# fashions/bike.rb class Motorbike < Car WHEELS = 2 finish
# Rails Console Automotive.create(identify: "Civic") Motorbike.create(identify: "Hayabusa") automobile = Motorbike.first automobile.turns into(Automotive) automobile.turns into!(Automotive) automobile.save