Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save gregblake/ad3a5996dfb6fb3135120d8ee74f2ba6 to your computer and use it in GitHub Desktop.

Select an option

Save gregblake/ad3a5996dfb6fb3135120d8ee74f2ba6 to your computer and use it in GitHub Desktop.
document_type_unique_validation_constraints.rb
[10] pry(main)> document_type = DocumentType.new
=> #<DocumentType:0x007faaa05e92d0
id: nil,
code: nil,
name: nil,
description: nil,
owner_id: nil,
active: nil,
created_at: nil,
updated_at: nil,
sort_order: nil,
has_restricted_pricing: false,
photo_only: false,
nitro_only: false,
use_for_project: false,
use_for_user: false,
expected_pages: nil,
width: nil,
height: nil,
models_and_styles: nil,
use_for_event: false,
can_be_reviewed: false,
[11] pry(main)> document_type.valid?
[write] DocumentType Exists (5.1ms) SELECT 1 AS one FROM `document_types` WHERE `document_types`.`name` IS NULL LIMIT 1
[write] DocumentType Exists (0.8ms) SELECT 1 AS one FROM `document_types` WHERE `document_types`.`code` IS NULL LIMIT 1
=> false
[12] pry(main)> document_type.name = "Foo"
=> "Foo"
[13] pry(main)> document_type.valid?
[write] DocumentType Exists (1.8ms) SELECT 1 AS one FROM `document_types` WHERE `document_types`.`name` = BINARY 'Foo' LIMIT 1
[write] DocumentType Exists (1.1ms) SELECT 1 AS one FROM `document_types` WHERE `document_types`.`code` IS NULL LIMIT 1
=> false
[14] pry(main)> document_type.errors.full_messages
=> ["Code can't be blank", "Code has already been taken"]
[15] pry(main)> document_type.code = "foo"
=> "foo"
[16] pry(main)> document_type.valid?
[write] DocumentType Exists (1.3ms) SELECT 1 AS one FROM `document_types` WHERE `document_types`.`name` = BINARY 'Foo' LIMIT 1
[write] DocumentType Exists (1.0ms) SELECT 1 AS one FROM `document_types` WHERE `document_types`.`code` = BINARY 'foo' LIMIT 1
=> true
[17] pry(main)> document_type.save!
[write] (0.7ms) BEGIN
[write] DocumentType Exists (2.2ms) SELECT 1 AS one FROM `document_types` WHERE `document_types`.`name` = BINARY 'Foo' LIMIT 1
[write] DocumentType Exists (1.7ms) SELECT 1 AS one FROM `document_types` WHERE `document_types`.`code` = BINARY 'foo' LIMIT 1
[write] SQL (1.8ms) INSERT INTO `document_types` (`code`, `name`, `description`, `owner_id`, `active`, `created_at`, `updated_at`, `sort_order`, `has_restricted_pricing`, `photo_only`, `nitro_only`, `use_for_project`, `use_for_user`, `expected_pages`, `width`, `height`, `models_and_styles`, `use_for_event`, `can_be_reviewed`, `review_start_date`, `digital_signatures`, `parent_document_type_id`, `requires_only_one_homeowner_signature`) VALUES ('foo', 'Foo', NULL, NULL, NULL, '2018-02-21 19:35:20', '2018-02-21 19:35:20', NULL, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, 0, 0, NULL, 0, NULL, 0)
[write] (0.6ms) COMMIT
=> true
[18] pry(main)> second_document_type = DocumentType.new
=> #<DocumentType:0x007faa87251410
id: nil,
code: nil,
name: nil,
description: nil,
owner_id: nil,
active: nil,
created_at: nil,
updated_at: nil,
sort_order: nil,
has_restricted_pricing: false,
photo_only: false,
nitro_only: false,
use_for_project: false,
use_for_user: false,
expected_pages: nil,
width: nil,
height: nil,
models_and_styles: nil,
use_for_event: false,
can_be_reviewed: false,
[22] pry(main)> second_document_type.code = "foo"
=> "foo"
[23] pry(main)> second_document_type.name = "Foo"
=> "Foo"
[24] pry(main)> second_document_type.valid?
[write] DocumentType Exists (1.4ms) SELECT 1 AS one FROM `document_types` WHERE `document_types`.`name` = BINARY 'Foo' LIMIT 1
[write] DocumentType Exists (1.3ms) SELECT 1 AS one FROM `document_types` WHERE `document_types`.`code` = BINARY 'foo' LIMIT 1
=> false
[25] pry(main)> second_document_type.errors.full_messages
=> ["Name has already been taken", "Code has already been taken"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment