1

我是 gem 改革的新用户。我的目标是更新一个名为的表shared_files及其关联表之一shared_files_user_accounts。我猜这个更新对两者都很好,除了改革在某种程度上试图更新另一个名为 的关联表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 ...

非常感谢!

4

0 回答 0