Ruby מצא מערך של מזהי משתמשים

סיכום מבוסס בינה מלאכותית

In a Ruby on Rails development discussion titled ‘Ruby find array of users ID,’ bXunter seeks to update passwords for a specific subset of users (around 1500) identified by an array of IDs, not all users. While pfaffman suggests User.all.update_all(password: '...') or looping through arrays, bXunter clarifies that only selected users (with empty password_hash/salt) need updating for migration purposes — these users can’t reset passwords via normal flow. pfaffman recommends User.where with PostgreSQL array syntax. Falco provides a working example: User.where('id IN (1,2,3,4,5)').find_each { |u| u.password = 'something' }. mstm asks if ID ranges (e.g., 100 to 300) are possible, and Falco confirms this is standard SQL-compatible. The thread offers practical solutions for bulk password updates targeting specific user IDs using SQL IN clauses or ranges, avoiding full-table updates.

Hello,

I need this command:

u = User.find_by_id('150)
u.password='some_password'
u.save!

And this is working just fine for one ID/user, the thing is that I have around 1500 user (I have list/array of theirs ID’s) and dont know how to list them all.

What I want to accomplish… need to update bulk password (same password testing only) for all users. I already do for one and it works, now I need quick method.

This might do it.

User.all.update_all(password: 'not a good idea')

You can impersonate users. So maybe you don’t need do do this?

Or you can Google “ruby loop array” for an example of how loops work.

And you can do User.find(id) and save a few keystrokes.

Hey Jay… the thing is that I dont need to update all users… just some of them with random set of IDs.

I’ve try with u = User.find_by_id([150,152,167,258,665])

But that always return for 150 ID and change password for only that User. Also try with User.find(id: 150,152....) didnt help…

Note: I’m trying to migrate the forum to another disscusion platform, and there are a number of users who have written a bunch of messages but are no longer coming to the forum (and for some reason the password_hash and salt columns are empty for those users, the user import script does not allow importing users who do not have these values ​​defined in the PGSQL database.), so I can’t let them reset their passwords. What I can do is that I set a complicated password, and they can certainly go to the Forgotten password option and reset a new one later.

User.where might be what you want and then the postgres syntax for finding stuff that matches an array. I can never quite remember.

User.where('id IN (1,2,3,4,5)').find_each do |u|
  u.password = 'something'
end

Is it also possible to use a range of users ID, for example from 100 to 300 ?
Thanks

Yes, it’s standards compliant SQL.