How can I run destroy_all on an array?
I’ve got the follow query:
spam_users = User.find_by_sql("SELECT * FROM users WHERE email ~* '21cn.com'")
I’ve tried running spam_users.destroy_all but I get undefined method 'destroy_all' for #<Array:0x10b09ce30>.
I’m running Rails 2.3.8 in this particular app along with PostgreSQL.
You don’t execute
destroy_allon an array,destroy_allis a class method on your models. The following should kill off everything would be in yourspam_usersarray:You could also iterate over
spam_usersand destroy them one by one if you already had them for other purposes:You might want to adjust your regex a bit as well:
so that you’re looking for a literal
.rather than “any character” and anchor it to the end of the string. You could also use a%q(...)quoted string to reduce the escaping: