改革正在更新填充集合的错误表格

时间:2015-09-18 21:04:17

标签: ruby-on-rails postgresql reform

我是宝石改革的新用户。 我的目标是更新名为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 ...

非常感谢!

0 个答案:

没有答案