그래서 레일스에서 사용자 모델을 작성 중이며이 사용자 모델에는 연결된 전자 메일 주소 모델이 있습니다. 전자 메일 주소 모델에는 전자 메일의 고유성 제약 조건이 있습니다. 지금은 사용자가 accepts_nested_attributes_for : email_address하도록 설정했습니다. 이것은 생성에 큰 작동하지만 갱신에이 오류가 얻을 :고유성 제약 조건이있는 오류에 대해 nested_attributes_for를 업데이트하십시오.
u = User.create(:name => "foo", :new_password => "Passw0rd",
:email_address_attributes => {:email => "[email protected]"})
u.update({:name => "new name",
:email_address_attributes => {:email => "[email protected]"}})
가 어떻게이 이름 동안을 업데이트받을 수 있나요 : 나는 레일 콘솔에서이 작업을 수행하여이 버그를 다시 만들 수 있습니다
ActiveRecord::JDBCError: org.postgresql.util.PSQLException:
ERROR: duplicate key value violates unique constraint
"index_email_addresses_on_email"
을 email_address에 신경 쓰지 마라. 어느 것이 변하지 않았습니까?
일부 다른 노트와 코드 :
내가 이메일에 내 EMAIL_ADDRESS 인덱스를하고는 제외하고 이메일 주소를 확인하지 않으려면 내가 레일 4
class User < ActiveRecord::Base
belongs_to :email_address
validates :email_address, :presence => true
accepts_nested_attributes_for :email_address
end
class EmailAddress < ActiveRecord::Base
validates_format_of :email, :with => RFC822::EmailAddress
validates :email, :presence => true
has_one :user
end
이것은 효과가 있습니다. 중요한 것은 : id를 email_address_attributes에 추가하는 것이 었습니다. 나는 가지고 있었다 : 전자 우편 그러나 아닙니다 : id. 감사. – NateSHolland
문제가 없습니다! 내가 도울 수있어서 기뻐! –