Skip to content

Instantly share code, notes, and snippets.

@joshuaclayton
Created July 16, 2009 16:00
Show Gist options
  • Save joshuaclayton/148498 to your computer and use it in GitHub Desktop.
Save joshuaclayton/148498 to your computer and use it in GitHub Desktop.
# not all callbacks should be put in an observer, just things not directly associated with the model itself:
class UserObserver < ActiveRecord::Observer
def after_create(user)
ClearanceMailer::deliver_user_credentials(user)
end
end
# callbacks are still cool in AR models if it deals with the class itself
class User < ActiveRecord::Base
before_validation_on_create :generate_random_password,
:if => lambda {|user| user.password.blank? }
protected
def generate_random_password
self.password = self.password_confirmation = ActiveSupport::SecureRandom.hex(6)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment