How to use observers in Ruby on Rails

    We can use observers with model to seperate some of the required functionality on paticular action To create a observer create observer file in models directory for example i have to create a observer for User model i can name it user_observer.rb it will send email on registration i have already created a mailer CustomMailer just call it here passing user object in it
    # models/user_observer.rb
    class UserObserver < ActiveRecord::Observer

      def after_create(user)
        # Send Registration Email
    To enable this observer place below code in your environment file in config/environments/ directory
      #Adding observers in config/environments/development.rb
      config.active_record.observers = :user_observer
    Average: 5 (1 vote)