0

속성 변경 조건을 가진 after_update 콜백이 여러 개 있으면 첫 번째 트리거 만 발생합니다. 모델 객체가 업데이트 될 때 속성 변경 조건을 가진 여러 after_update 콜백이 첫 번째 호출 만 수행합니다.

class Article < ActiveRecord::Base 
    after_update :method_1, :if => proc{ |obj| obj.status_changed? && obj.status == 'PUBLISHED' } 
    after_update :method_2, :if => proc{ |obj| obj.status_changed? && obj.status == 'PUBLISHED' && obj.name == 'TEST' } 
    ... 
end 

method_1

가 트리거됩니다 :

Article.last.update_attributes(status: 'PUBLISHED', name: 'TEST') 

method_2 동안 트리거되지 않습니다.

답변

1

if...end 블록이있는 콜백을 사용하여 각 경우에 수행하려는 작업을 필터링 할 수 있습니다.

class Article < ActiveRecord::Base 
    after_update :method_1, :if => proc{ |obj| obj.status_changed? && obj.status == 'PUBLISHED' } 
    ... 

    def method_1   
    if self.name == 'TEST' 
     # stuff you want to do on method_2 
    else 
     # stuff you want to do on method_1 
    end 
    end 
end 
0

콜백의 반환 값을 확인하십시오. before_* 또는 after_* ActiveRecord 콜백이 false를 반환하면 체인의 모든 이후 콜백이 취소됩니다. docs (취소 콜백 참조) 발

If a before_* callback returns false, all the later callbacks and the associated action are cancelled. If an after_* callback returns false, all the later callbacks are cancelled.