RecordNotUnique Exception - PullRequest
       8

RecordNotUnique Exception

0 голосов
/ 27 июня 2019

Я получаю сообщение об ошибке вроде ActiveRecord::RecordNotUnique Exception в моем has_many через ассоциацию

class Customer < ActiveRecord::Base
  has_many :customers_sellers, dependent: :destroy
  has_many :sellers, through: :customers_sellers
end 

class Seller < ActiveRecord::Base
  has_many :customers_sellers, dependent: :destroy
  has_many :customers, through: :customers_sellers
end 

class CustomersSeller < ActiveRecord::Base
  belongs_to :customer
  belongs_to :seller
end

При создании клиента для конкретного продавца я получаю сообщение об ошибке CustomersSeller.create() строка.

Error: ActiveRecord::RecordNotUnique Exception 

В моем has_many через ассоциацию.

Дубликат ключа нарушает уникальное ограничение "Customers_sellers_pkey".

1 Ответ

0 голосов
/ 28 июня 2019

Это исправило мою проблему:

    begin
          CustomersSeller.where(seller_id: @seller.id, customer_id: @customer.id).first_or_create(seller: @seller, customer: @customer, customer_name: @customer.try(:business_name), status: "Invited")
  rescue ActiveRecord::RecordNotUnique
    retry
  end
...