Validate uniqueness of Contact#value
This commit is contained in:
parent
5fc557f741
commit
2d6a930bb8
4 changed files with 20 additions and 1 deletions
|
@ -23,7 +23,9 @@ class Contact < ApplicationRecord
|
|||
|
||||
validates :contact_network, presence: true
|
||||
|
||||
validates :value, presence: true
|
||||
validates :value,
|
||||
presence: true,
|
||||
uniqueness: { scope: %i[contact_list_id contact_network_id] }
|
||||
|
||||
private
|
||||
|
||||
|
|
|
@ -178,6 +178,10 @@ private
|
|||
t.references :contact_network, null: false, index: true, foreign_key: true
|
||||
|
||||
t.string :value, null: false
|
||||
|
||||
t.index %i[contact_list_id contact_network_id value],
|
||||
name: :index_contacts_on_list_id_and_network_id_and_value,
|
||||
unique: true
|
||||
end
|
||||
|
||||
create_table :federal_subjects do |t|
|
||||
|
|
|
@ -1156,6 +1156,13 @@ CREATE INDEX index_contacts_on_contact_list_id ON public.contacts USING btree (c
|
|||
CREATE INDEX index_contacts_on_contact_network_id ON public.contacts USING btree (contact_network_id);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_contacts_on_list_id_and_network_id_and_value; Type: INDEX; Schema: public; Owner: -
|
||||
--
|
||||
|
||||
CREATE UNIQUE INDEX index_contacts_on_list_id_and_network_id_and_value ON public.contacts USING btree (contact_list_id, contact_network_id, value);
|
||||
|
||||
|
||||
--
|
||||
-- Name: index_federal_subjects_on_english_name; Type: INDEX; Schema: public; Owner: -
|
||||
--
|
||||
|
|
|
@ -24,6 +24,12 @@ RSpec.describe Contact do
|
|||
|
||||
it { is_expected.to validate_presence_of :value }
|
||||
|
||||
it do
|
||||
is_expected.to \
|
||||
validate_uniqueness_of(:value)
|
||||
.scoped_to(:contact_list_id, :contact_network_id)
|
||||
end
|
||||
|
||||
it { is_expected.not_to allow_value nil }
|
||||
it { is_expected.not_to allow_value '' }
|
||||
it { is_expected.not_to allow_value ' ' * rand(1..3) }
|
||||
|
|
Reference in a new issue