Skip to content

Instantly share code, notes, and snippets.

@erichurst
Created February 2, 2012 17:40
Show Gist options
  • Save erichurst/1724799 to your computer and use it in GitHub Desktop.
Save erichurst/1724799 to your computer and use it in GitHub Desktop.
Custom callback notification with Rails 3
class User < ActiveRecord::Base
...
def self.new_sitter(user, params)
if user.parent?
transaction do
sitter = create! do |u|
u.sub_type = 'sitter'
u.first_name = params[:first_name]
u.last_name = params[:last_name]
u.email = params[:email]
end
notify_observers :new_sitter_created
sitter
end
else
logger.info "User: #{user.id}, #{user.to_s}, with sub_type 'sitter' attempted to create a User with sub_type 'sitter'."
nil
end
end
...
end
class UserObserver < ActiveRecord::Observer
def new_sitter_created(user)
#do something
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment