私はRails 4アプリケーションでポリモーフィックな関連付けを設定し続けると思われる問題を見つけようとしています。Rails 4 - ネストされた多態性属性に対する更新アクションを伴う多相関連
私はプロジェクトモデルとアドレスモデルを持っています。団体は以下のとおりです。私は以前、同じ問題にこの質問をした
belongs_to :addressable, :polymorphic => true
プロフィール
has_many :addresses, as: :addressable
accepts_nested_attributes_for :addresses, reject_if: :all_blank, allow_destroy: 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エラーであるの種類を与えることができます
希望は、それがすでに「1」として、「プロファイル」とaddressable_idなどaddresable_typeと、データベース内のエントリがあることを意味しています。つまり、更新アクションで新しいアドレス指定可能なものを作成しているということです。 – nzajt
私はそれをどのようにして既存のレコードのアップデートにすることができるか知っていますか? – Mel