Rails Observer Alternatives for 4.0

前端 未结 12 785
伪装坚强ぢ
伪装坚强ぢ 2020-12-04 05:29

With Observers officially removed from Rails 4.0, I\'m curious what other developers are using in their place. (Other than using the extracted gem.) While Observers were cer

12条回答
  •  失恋的感觉
    2020-12-04 05:53

    I have the same probjem! I find a solution ActiveModel::Dirty so you can track your model changes!

    include ActiveModel::Dirty
    before_save :notify_categories if :data_changed? 
    
    
    def notify_categories
      self.categories.map!{|c| c.update_results(self.data)}
    end
    

    http://api.rubyonrails.org/classes/ActiveModel/Dirty.html

提交回复
热议问题