Home > Back-end >  How can I list devise users in a ruby on rails project using the console?
How can I list devise users in a ruby on rails project using the console?

Time:12-26

I have a ruby on rails project, and I am using devise for user authentication. Is there any way for me to check for the existence of an account by email using the rails console? If not-- is there a way for me to list all users on the app in the console (similar to how I can list all instances of a DB model)?

I have a list of emails, and I want to know if there are corresponding accounts with such emails.

CodePudding user response:

You can run database queries in your Rails console just in the same way as in your controller.

Imaging you have a User model that has an email attribute and the email you want to check are in an emails variable then you can just run the following lines in your Rails console:

emails = %w[[email protected] [email protected]] 
User.where(email: emails).order(:email).pluck(:email)

Which would return all emails that were found in the database. Or

emails = %w[[email protected] [email protected]] 
found_emails = User.where(email: emails).order(:email).pluck(:email)
emails - found_emails

Which would return all emails that were not found in the database.

  • Related