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ブロックで1つのコールバックを使用するだけで、それぞれの場合に実行する操作をフィルタすることができます。

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. 
関連する問題