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.

  • If yes, update the role col in that record to Manager,
  • else insert a new record.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top