2012-10-03 2 views
0

잎. 나는이 수행 할 때것은 내가 모델이 FK

: 데이터베이스에서

merchant.billing_address.destroy 

를, 주소 기록은 사라지고 있지만, merchants.billing_address_id는 가짜 값을 유지합니다. 이것은 mysql이므로 참조 무결성은 없다.

내가 뭘 잘못하고 있니?

참고 :이 모델은 has_one 연관으로 더 잘 모델링 될 수 있습니다. 나는 거기에 가야 할지도 모르지만 나는하지 않는 것을 좋아한다.

업데이트 : 여러 개의 주소 연결을 표시하는 코드가 조금 추가되었습니다.

답변

0

ActiveRecord는 일방의 belongs_to 연관을 지원하지 않습니다. unlike other ORMs I've worked with. 하지만 당신과 같이 스스로를 해킹 할 수

before_save :remove_ids_for_nil_associations 

def remove_ids_for_nil_associations 
    self.billing_address_id = nil if is_getting_destroyed? self.billing_address 
    ... 
end 

def is_getting_destroyed?(ref) 
    ref.present? && ref.destroyed? 
end