# email.rb
serialize :receive_permissions, Array
2.3.1 :042 > Email.find_or_create_by!(code: "TestEmail02", title: "TestEmail_02", receive_permissions: ["country", "high_end"])
Email Load (0.4ms) SELECT "emails".* FROM "emails" WHERE "emails"."code" = $1 AND "emails"."title" = $2 AND "emails"."receive_permissions" IN ('country', 'high_end') LIMIT 1 [["code", "TestEmail02"], ["title", "TestEmail_02"]]
(0.2ms) BEGIN
Email Exists (0.4ms) SELECT 1 AS one FROM "emails" WHERE "emails"."code" = 'TestEmail02' LIMIT 1
Email Exists (0.3ms) SELECT 1 AS one FROM "emails" WHERE "emails"."title" = 'TestEmail_02' LIMIT 1
SQL (0.3ms) INSERT INTO "emails" ("code", "title", "receive_permissions", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5)
RETURNING "id" [["code", "TestEmail02"], ["title", "TestEmail_02"], ["receive_permissions", "---\n- country\n- high_end\n"], ["created_at", "2019-10-10 17:59:54.716189"], ["updated_at", "2019-10-10 17:59:54.716189"]]
(1.6ms) COMMIT
=> #<Email id: 29, code: "TestEmail02", title: "TestEmail_02", description: nil, receive_permissions: ["country", "high_end"], created_at: "2019-10-10 09:59:54", updated_at: "2019-10-10 09:59:54">
2.3.1 :043 > Email.find_or_create_by!(code: "TestEmail02", title: "TestEmail_02", receive_permissions: ["country", "high_end"])
Email Load (0.4ms) SELECT "emails".* FROM "emails" WHERE "emails"."code" = $1 AND "emails"."title" = $2 AND "emails"."receive_permissions" IN ('country', 'high_end') LIMIT 1 [["code", "TestEmail02"], ["title", "TestEmail_02"]]
(0.2ms) BEGIN
Email Exists (0.3ms) SELECT 1 AS one FROM "emails" WHERE "emails"."code" = 'TestEmail02' LIMIT 1
Email Exists (0.4ms) SELECT 1 AS one FROM "emails" WHERE "emails"."title" = 'TestEmail_02' LIMIT 1
(0.2ms) ROLLBACK
ActiveRecord::RecordInvalid: Validation failed: Code has already been taken, Title has already been taken
from /Users/iewad/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.3/lib/active_record/validations.rb:79:in `raise_record_invalid'
from /Users/iewad/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.3/lib/active_record/validations.rb:43:in `save!'
from /Users/iewad/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.3/lib/active_record/attribute_methods/dirty.rb:29:in `save!'
from /Users/iewad/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.3/lib/active_record/transactions.rb:291:in `block in save!'
from /Users/iewad/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.3/lib/active_record/transactions.rb:351:in `block in with_transaction_returning_status'
from /Users/iewad/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
from /Users/iewad/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
from /Users/iewad/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
from /Users/iewad/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.3/lib/active_record/transactions.rb:220:in `transaction'
from /Users/iewad/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.3/lib/active_record/transactions.rb:348:in `with_transaction_returning_status'
from /Users/iewad/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.3/lib/active_record/transactions.rb:291:in `save!'
from /Users/iewad/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.3/lib/active_record/persistence.rb:51:in `create!'
from /Users/iewad/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.3/lib/active_record/relation.rb:151:in `block in create!'
from /Users/iewad/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.3/lib/active_record/relation.rb:302:in `scoping'
from /Users/iewad/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.3/lib/active_record/relation.rb:151:in `create!'
from /Users/iewad/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.3/lib/active_record/relation.rb:219:in `find_or_create_by!'
from /Users/iewad/.rvm/gems/ruby-2.3.1/gems/activerecord-4.2.3/lib/active_record/querying.rb:6:in `find_or_create_by!'
from (irb):43
from /Users/iewad/.rvm/gems/ruby-2.3.1/gems/railties-4.2.3/lib/rails/commands/console.rb:110:in `start'
from /Users/iewad/.rvm/gems/ruby-2.3.1/gems/railties-4.2.3/lib/rails/commands/console.rb:9:in `start'
from /Users/iewad/.rvm/gems/ruby-2.3.1/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/iewad/.rvm/gems/ruby-2.3.1/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/iewad/.rvm/gems/ruby-2.3.1/gems/railties-4.2.3/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'