diff --git a/app/models/quantity.rb b/app/models/quantity.rb index 89c47b5..32cc0d0 100644 --- a/app/models/quantity.rb +++ b/app/models/quantity.rb @@ -9,7 +9,8 @@ class Quantity < ApplicationRecord validate if: ->{ parent.present? } do errors.add(:parent, :user_mismatch) unless user == parent.user end - validates :name, presence: true, length: {maximum: type_for_attribute(:name).limit} + validates :name, presence: true, uniqueness: {scope: [:user_id, :parent_id]}, + length: {maximum: type_for_attribute(:name).limit} validates :description, length: {maximum: type_for_attribute(:description).limit} scope :defaults, ->{ where(user: nil) } diff --git a/db/migrate/20250104194343_create_quantities.rb b/db/migrate/20250104194343_create_quantities.rb index 1cdb7bc..1bb31d5 100644 --- a/db/migrate/20250104194343_create_quantities.rb +++ b/db/migrate/20250104194343_create_quantities.rb @@ -2,12 +2,12 @@ class CreateQuantities < ActiveRecord::Migration[7.2] def change create_table :quantities do |t| t.references :user, foreign_key: true - t.integer :domain t.string :name, null: false, limit: 31 t.text :description t.references :parent, foreign_key: {to_table: :quantities} t.timestamps null: false end + add_index :quantities, [:user_id, :parent_id, :name], unique: true end end diff --git a/db/schema.rb b/db/schema.rb index 213db6f..583deb0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -13,13 +13,13 @@ ActiveRecord::Schema[7.2].define(version: 2025_01_04_194343) do create_table "quantities", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.bigint "user_id" - t.integer "domain" t.string "name", limit: 31, null: false t.text "description" t.bigint "parent_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["parent_id"], name: "index_quantities_on_parent_id" + t.index ["user_id", "parent_id", "name"], name: "index_quantities_on_user_id_and_parent_id_and_name", unique: true t.index ["user_id"], name: "index_quantities_on_user_id" end