ruby - Rails 4 - Polymorphic associations with update action on nested polymorphic attributes -


i'm trying figure out problem seem keep having in setting polymorphic associations in rails 4 app.

i have project model , address model. associations are:

profile

has_many :addresses, as: :addressable     accepts_nested_attributes_for :addresses,  reject_if: :all_blank, allow_destroy: true 

address

belongs_to :addressable, :polymorphic => true 

i asked question on same problem. couldn't (and still can't) understand answers in post: rails 4 - polymorphic associations

this time around - i'm having problem triggered when try update profile inserting address. error message identifies problem coming update action in profiles controller. update action has:

my profiles controller update action has:

def update      # successful = @profile.update(profile_params)      # rails.logger.info "xxxxxxxxxxxxx"     # rails.logger.info successful.inspect     # user=@profile.user     # user.update.avatar     # rails.logger.info "prof xxxxxxxxxxxxx"     # rails.logger.info @profile.update(profile_params)     respond_to |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 

the error message says:

error:  duplicate key value violates unique constraint "index_addresses_on_addressable_type_and_addressable_id" detail:  key (addressable_type, addressable_id)=(profile, 1) exists. 

does know message means, , how address it?

in database, have set unique constraint: , can go database see have set name of "index_addresses_on_addressable_type_and_addressable_id". error message show, try update record value ( profile , 1) has been used record.
solve issue, there 2 solutions: 1 database side: need know why there unique constraint addresses. if not need , can remove database. other ensure (addressable_type, addressable_id) unique before update data database.

hope can give kind of help


Comments