2016-08-09 2 views
0

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. 

사람은이 메시지가 무엇을 의미하는지 알고 있나요, 어떻게 그것을 해결하기 위해?

+0

이것은 PSQL 오류입니다의 종류를 제공 할 수 있습니다

희망은, 이미 "1"로 "프로파일"을 addressable_id로 addresable_type으로 데이터베이스에 항목이 있음을 의미한다. 즉, 업데이트 작업에서 새 주소 지정 가능을 만드는 중입니다. – nzajt

+0

기존 레코드의 업데이트를 어떻게 알 수 있습니까? – Mel

답변

0

데이터베이스에서 고유 제한 조건을 설정했습니다. 데이터베이스로 이동하여 "index_addresses_on_addressable_type_and_addressable_id"이름으로 설정된 항목을 확인할 수 있습니다. 오류 메시지가 표시되면 이미 다른 레코드에서 사용 된 값 (프로필, 1)으로 레코드를 업데이트하려고합니다.
이 문제를 해결하려면 두 가지 해결책이 있습니다. 하나는 데이터베이스 측에서입니다. 주소에 대한 고유 한 제한이있는 이유를 알아야합니다. 필요하지 않은 경우 데이터베이스에서 제거 할 수 있습니다. 다른 하나는 데이터를 데이터베이스로 업데이트하기 전에 (addressable_type, addressable_id)가 고유 한 것인지 확인해야합니다. 이 도움이

+0

감사합니다. @Eric - 왜 인덱스 값을 고유하게 만들 었는지 생각할 수 없습니다. add_index "addresses", [ "addressable_type", "addressable_id"], 이름 : "index_addresses_on_addressable_type_and_addressable_id", 고유 : true, 사용 : : tree 왜 그렇게했는지 알아 내려고하면 솔루션이 훌륭하게 들립니다. 감사! – Mel