我试图找出一个问题,我似乎在设置我的Rails 4应用程序中的多态关联。Rails 4 - 嵌套多态属性更新动作的多态关联
我有一个项目模型和地址模型。该协会是:
档案
has_many :addresses, as: :addressable
accepts_nested_attributes_for :addresses, reject_if: :all_blank, allow_destroy: true
地址
belongs_to :addressable, :polymorphic => true
我以前问过这个问题上同样的问题。我不能(也不能)理解那篇文章中的答案:Rails 4 - Polymorphic associations
这次 - 我遇到了一个问题,当我尝试通过插入地址更新配置文件时触发。该错误消息标识问题来自配置文件控制器中的更新操作。更新动作有:
我的配置控制器更新动作有:
def update
# successful = @profile.update(profile_params)
# Rails.logger.info "xxxxxxxxxxxxx"
# Rails.logger.info successful.inspect
# [email protected]
# user.update.avatar
# Rails.logger.info "prof xxxxxxxxxxxxx"
# Rails.logger.info @profile.update(profile_params)
respond_to do |format|
if @profile.update(profile_params)
format.html { redirect_to @profile }
format.json { render :show, status: :ok, location: @profile }
else
format.html { render :edit }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end
错误消息说:
ERROR: duplicate key value violates unique constraint "index_addresses_on_addressable_type_and_addressable_id"
DETAIL: Key (addressable_type, addressable_id)=(Profile, 1) already exists.
有谁知道这个消息意味着,以及如何解决它?
这是一个PSQL错误,这意味着已经存在与addresable_type作为“配置文件”和addressable_id为“1”数据库中的条目。所以,这意味着您正在更新操作中创建一个新的Addressable。 – nzajt
你知道我可以如何将它变成现有记录的更新吗? – Mel