Today I Learned

hashrocket A Hashrocket project

Authenticate With Username or Email

Today I learned a way to implement a login form that accepts email or username as the login. I've been on the other side of a form like this many times, but had never written one myself.

Here's one solution, with ActiveRecord:

User.where('username = ? or email = ?', "jwworth", "jake@example.com")

title or slug are represented by the same parameter, and either can be nil.

login = params.fetch('username_or_email')
User.where('username = ? or email = ?', login, login)
See More #rails TILs
Looking for help? Hashrocket has been an industry leader in Ruby on Rails since 2008. Rails is a core skill for each developer at Hashrocket, and we'd love to take a look at your project. Contact us and find out how we can help you.