From 0e7ee218377b6c19c3f72a1d61747c662201151a Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Sat, 24 Feb 2024 01:36:08 +0900 Subject: [PATCH] Let Room name be unique within each event --- app/models/room.rb | 2 +- spec/models/room_spec.rb | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/app/models/room.rb b/app/models/room.rb index 631134354..5f7e13f9e 100644 --- a/app/models/room.rb +++ b/app/models/room.rb @@ -2,7 +2,7 @@ class Room < ApplicationRecord belongs_to :event has_many :time_slots - validates :name, uniqueness: true, presence: true + validates :name, uniqueness: {scope: :event_id}, presence: true scope :by_grid_position, -> {where.not(grid_position: nil).order(:grid_position)} scope :grid_order, -> { order(:grid_position) } end diff --git a/spec/models/room_spec.rb b/spec/models/room_spec.rb index e45678970..dfe73267f 100644 --- a/spec/models/room_spec.rb +++ b/spec/models/room_spec.rb @@ -2,11 +2,15 @@ describe Room do describe "validations" do - it "requires that names are unique" do - room = create(:room, name: 'name') - room2 = build(:room, name: room.name) - + it "requires that names are unique within an event" do + event1 = create(:event) + room = create(:room, event: event1, name: 'name') + room2 = build(:room, event: event1, name: room.name) expect(room2).to be_invalid + + event2 = create(:event) + room3 = build(:room, event: event2, name: room.name) + expect(room3).to be_valid end end end