summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/models/cart.rb4
-rw-r--r--app/models/product.rb2
-rw-r--r--app/models/product_cart.rb8
-rw-r--r--db/migrate/20230405015505_create_product_carts.rb13
-rw-r--r--db/schema.rb13
-rw-r--r--spec/models/product_cart_spec.rb5
6 files changed, 43 insertions, 2 deletions
diff --git a/app/models/cart.rb b/app/models/cart.rb
index f9e5ea0..4bccd67 100644
--- a/app/models/cart.rb
+++ b/app/models/cart.rb
@@ -2,5 +2,7 @@
# Cart
class Cart < ApplicationRecord
- has_one :user_account
+ has_one :user_account, dependent: :destroy
+ has_many :product_carts
+ has_many :products, through: :product_carts
end
diff --git a/app/models/product.rb b/app/models/product.rb
index b9ca828..c4514a2 100644
--- a/app/models/product.rb
+++ b/app/models/product.rb
@@ -12,6 +12,8 @@ class Product < ApplicationRecord
belongs_to :company
has_many :product_reviews
+ has_many :product_carts
+ has_many :carts, through: :product_carts
validates :name, presence: true
validates :unitary_price, presence: true
diff --git a/app/models/product_cart.rb b/app/models/product_cart.rb
new file mode 100644
index 0000000..10d4a4c
--- /dev/null
+++ b/app/models/product_cart.rb
@@ -0,0 +1,8 @@
+# frozen_string_literal: true
+
+# ProductCart
+# quantity: integer
+class ProductCart < ApplicationRecord
+ belongs_to :cart
+ belongs_to :product
+end
diff --git a/db/migrate/20230405015505_create_product_carts.rb b/db/migrate/20230405015505_create_product_carts.rb
new file mode 100644
index 0000000..4cafb27
--- /dev/null
+++ b/db/migrate/20230405015505_create_product_carts.rb
@@ -0,0 +1,13 @@
+class CreateProductCarts < ActiveRecord::Migration[7.0]
+ def change
+ create_table :product_carts do |t|
+ t.integer :quantity
+ t.belongs_to :cart, index: true
+ t.belongs_to :product, index: true
+
+ t.timestamps
+ end
+
+ add_index(:product_carts, [:product_id, :cart_id], unique: true)
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 5c4292f..b974771 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema[7.0].define(version: 2023_04_05_013826) do
+ActiveRecord::Schema[7.0].define(version: 2023_04_05_015505) do
create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_general_ci", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
@@ -76,6 +76,17 @@ ActiveRecord::Schema[7.0].define(version: 2023_04_05_013826) do
t.datetime "updated_at", null: false
end
+ create_table "product_carts", charset: "utf8mb4", collation: "utf8mb4_general_ci", force: :cascade do |t|
+ t.integer "quantity"
+ t.bigint "cart_id"
+ t.bigint "product_id"
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.index ["cart_id"], name: "index_product_carts_on_cart_id"
+ t.index ["product_id", "cart_id"], name: "index_product_carts_on_product_id_and_cart_id", unique: true
+ t.index ["product_id"], name: "index_product_carts_on_product_id"
+ end
+
create_table "product_reviews", charset: "utf8mb4", collation: "utf8mb4_general_ci", force: :cascade do |t|
t.bigint "user_account_id", null: false
t.bigint "product_id", null: false
diff --git a/spec/models/product_cart_spec.rb b/spec/models/product_cart_spec.rb
new file mode 100644
index 0000000..7a1a676
--- /dev/null
+++ b/spec/models/product_cart_spec.rb
@@ -0,0 +1,5 @@
+require 'rails_helper'
+
+RSpec.describe ProductCart, type: :model do
+ pending "add some examples to (or delete) #{__FILE__}"
+end