Skip to content

Instantly share code, notes, and snippets.

@uberllama

uberllama/gist:7505406

Last active Dec 28, 2015
Embed
What would you like to do?
Observer example 1
# config/application.rb
config.autoload_paths << "#{config.root}/app/models/observers"
config.active_record.observers = :user_observer
# app/models/user.rb
class User < ActiveRecord::Base
has_many :galleries
end
# app/models/observers/user_observer.rb
class UserObserver < ActiveRecord::Observer
def after_create(user)
create_default_gallery(user)
send_welcome_email(user)
end
private
def create_default_gallery(user)
user.galleries.create(name: "Default gallery")
end
def send_welcome_email(user)
UserMailer.delay.welcome(user.id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.