Skip to content

Instantly share code, notes, and snippets.

@sobstel
Created September 28, 2011 21:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sobstel/1249314 to your computer and use it in GitHub Desktop.
Save sobstel/1249314 to your computer and use it in GitHub Desktop.
Devise: find by username OR email
class User
extend ClassMethods
devise :database_authenticatable, :omniauthable, :registerable, :confirmable, :recoverable, :rememberable
module ClassMethods
def find_for_database_authentication(conditions)
value = conditions[authentication_keys.first]
where(["username = :value OR email = :value", { :value => value }]).first
end
def send_reset_password_instructions(attributes={})
super(authentication_attribute(attributes))
end
def send_confirmation_instructions(attributes={})
super(authentication_attribute(attributes))
end
protected
# determine email by username
def authentication_attribute(attributes)
user = self.find_by_username(attributes[:email])
attributes[:email] = user[:email] if user
attributes
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment