Реформа обновляет неправильную таблицу при заполнении коллекциями

Я новый пользователь Gem Reform. Моя цель - обновить таблицу с именем shared_files и одна из связанных с ней таблиц shared_files_user_accounts, Я думаю, что обновление работает хорошо для обоих, за исключением того, что Reform пытается обновить другую связанную с ней таблицу с именем user_accountsкоторый не должен. Мои свойства следующие:

class SharedFileUploadForm < Reform::Form 
model :shared_file
property :filename
property :file, virtual: true
collection :user_accounts,
          populate_if_empty: lambda {|fragment, _|
            user_account= UserAccount.filter(email_address: fragment["email_address"])
          } do
  property :email_address, validates: { presence: true }  
end
validate :only_existing_user_accounts

def only_existing_user_accounts
  return if user_accounts.all?(&:persisted?)
  errors.add(:user_accounts, "must already exist")
 end
end

Тогда в shared_files модель, я обновляю данные в shared_files_user_accounts:

  def user_accounts=(collection)
    shared_file_user_accounts_dataset.delete

    add_shared_file_user_account(SharedFileUserAccount.new(shared_file_id: shared_file_id, user_account_id: owner_id))

    collection.each do |user_account|
      add_shared_file_user_account(SharedFileUserAccount.new(shared_file_id: shared_file_id, user_account_id: user_account.id))
    end
  end

Вот сообщение об ошибке от postgre:

[ERROR] unicorn worker[0] --port xxxx:yyyy - PG::UniqueViolation: 
ERROR:  duplicate key value violates unique constraint    "user_accounts_email_address_key"
DETAIL:  Key (email_address)=(EMAIL@ADDRESS) already exists.: UPDATE "user_accounts" SET ...

Большое спасибо!

0 ответов

Другие вопросы по тегам