2010-05-10 3 views
1

모델 A와 모델 첨부가 있습니다. 중첩 된 특성이있는 A 양식을 다음과 같이 편집합니다. 그리고 accepts_nested_attributes_for를 통해 A에서 모든 첨부 파일을 삭제할 때 중첩 된 모든 모델에 대해 after_update/after_save 콜백을 어떻게 얻을 수 있습니까? 문제는 모델 A에서 콜백을 실행할 때 모델 A가 업데이트되고 모델 부착이 업데이트 된 후에 실행된다는 것입니다. 예를 들어, 내가 모두 삭제 한 후에 첨부 파일이 없는지를 알 수 없습니다. :) .has_many 연결, 중첩 모델 및 콜백

예를 들어 내 첨부 파일을 모두 삭제 한 후에 나의 콜백 after_save :update_status이 제대로 작동하지 않습니다.

model A 
    after_save :update_status 
    has_many :attaches 
    accepts_nested_attributes_for :attaches, :reject_if => proc { |attributes| attributes['file'].blank? }, :allow_destroy => true 

    def update_status 
    print "\n\nOUPS! bag is empty!\n\n" if self.attaches.empty? 
    end 
end 

model Attach 
    belongs_to A 
end 

내가 rubyonrails.org에서 레일 3에게 베타

답변

0

좋아, 나는 중첩 된 모델 Attach (after_destroy 콜백)

model A 
    has_many :attaches 
    accepts_nested_attributes_for :attaches, :reject_if => proc { |attributes| attributes['file'].blank? }, :allow_destroy => true 
end 

model Attach 
    after_destroy :update_status 
    belongs_to :a 

    def update_status 
    print "\n\nOUPS! bag is empty!\n\n" if self.a.attaches.empty? 
    end 
end 
2

을 사용하고 있습니다 : 중요

:을 위해 상속에 대한 콜백 큐 작동하려면 지정하기 전에 콜백을 지정해야합니다 협회. 그렇지 않으면 부모가 콜백을 등록하기 전에 자식로드를 트리거 할 수 있으며 은 상속되지 않습니다.

아니요? 콜백 전에 연결을 지정하고 있습니다.

+0

좋은 통보에 A에서 콜백 after_save 제거했지만, 내 진짜 모델 콜백에 코드 전에 지정 – fl00r