Note that there are some explanatory texts on larger screens.

plurals
  1. PODevise: How to quickly reset all user passwords
    primarykey
    data
    text
    <p>I have a rake task which (among other things) clones my production database to my local machine and resets all the users passwords to <code>password</code> (or whatever). </p> <p>My current implementation looks something like:</p> <pre><code>User.find_each do |user| user.password = 'password' user.save end </code></pre> <p>And that works fine. But now that we have over 1000 users it's getting pretty slow, and it's sure to get worse. </p> <p><strong>EDIT</strong></p> <p>Okay. Here's a partial answer:</p> <pre><code>Devise.stretches = 1 User.find_each do |user| user.password = 'password' user.save end Devise.stretches = 10 </code></pre> <p>This got me about a 5-10x speed increase. While it's still probably slow compared to a SQL-based solution, it's still a very nice improvement. This should scale to at least 10,000 users.</p> <p>I may still mess around with a SQL solution, if I have time.</p> <p>I'll leave this question open for a bit. If someone else has a better solution, please post.</p> <h3>Final Answer / Best Solution</h3> <p>As several comments have suggested, the fastest solution is to execute a mass update via SQL. Devise now allows you to set the <code>encrypted_password</code> directly:</p> <pre><code>sample_user = User.last sample_user.password = "password" encrypted_password = sample_user.encrypted_password User.update_all(encrypted_password: encrypted_password) </code></pre> <p>Basically, we set the password on a single user, then we can use their <code>encrypted_password</code> to execute a mass update. This solution should scale to virtually any number of users. </p> <p><em>Credit to @vladCovaliov for suggesting this solution in a comment below.</em></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload