ActiveRecord Global Callbacks for all Models

前端 未结 5 837
清酒与你
清酒与你 2020-12-31 20:06

I have around 40 models in my RoR application. I want to setup a after_save callback for all models. One way is to add it to all models. Since this callback has the same cod

相关标签:
5条回答
  • 2020-12-31 20:31

    Based on @harish's answer and in this answer (https://stackoverflow.com/a/10712838/2226338):

    class AuditObserver < ActiveRecord::Observer
        Rails.application.eager_load!
        observe ActiveRecord::Base.descendants
    
        def after_save(record)
            ...
        end
    end
    
    0 讨论(0)
  • 2020-12-31 20:40

    I'm pretty late on this one, but in case someone else is using Rails 3 and finds this, then this response might help.

    Some models might not be loaded when the observer is loaded. The documentation says that you can override observed_classes, and that way you can get the subclasses of active record dynamically.

    class AuditObserver < ActiveRecord::Observer
      def self.observed_classes                 
        ActiveRecord::Base.send(:subclasses)    
      end
    end
    
    0 讨论(0)
  • 2020-12-31 20:45

    This seemed to work for me:

    ActiveRecord::Base.after_save do
       ...
    end
    

    Is there a problem I'm not seeing?

    0 讨论(0)
  • 2020-12-31 20:50

    This actually works pretty well for me in 2.3.8:

    class AudiObserver < ActiveRecord::Observer
      observe :'ActiveRecord::Base'
      #
      # observe methods...
      #
    end
    
    0 讨论(0)
  • 2020-12-31 20:53

    You should use observers for this:

    class AuditObserver < ActiveRecord::Observer      
    
      observe ActiveRecord::Base.send(:subclasses)
    
      def after_save(record)
        AuditTrail.new(record, "UPDATED")
      end
    end
    

    In order to activate an observer, list it in the config.active_record.observers configuration setting in your config/application.rb file.

    config.active_record.observers = :audit_observer
    

    Note

    In Rails 4, the observer feature is removed from core. Use the https://github.com/rails/rails-observers gem.

    0 讨论(0)
提交回复
热议问题