I have such situation: I have Users table and I divide users by type: Admins, Managers, Customer.
Managers can be created only with Organizer model( organizer has_many managers) and it is creating on nested form.
I want to upgrade Customer to Manager if someone is trying to create Manager with already registered Customer's email.
Current situation:
When I'm trying to create Manager with Customer's email it showing me database error. I can make some validation to change Customer type -> to -> Manager, but it still wants to save Manager record and obviously failing and giving me next error.
ActiveRecord::RecordNotUnique:
PG::Error: ERROR: duplicate key value violates unique constraint "index_users_on_email"
How I can handle this RIGHT ?
P.S. Here is my models code:
class Organizer
accepts_nested_attributes_for :managers
has_many :managers
___________________
class Manager < User
belongs_to :organizer
___________________
class Customer < User
You should first check if such a record with specified email ID exists in your Users table.