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_all
on an array, destroy_all
is a class method on your models. The following should kill off everything would be in your spam_users
array:
User.destroy_all("email ~* '21cn.com'")
You could also iterate over spam_users
and destroy them one by one if you already had them for other purposes:
spam_users.each(&:destroy)
You might want to adjust your regex a bit as well:
User.destroy_all("email ~* '21cn\\.com$'")
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:
User.destroy_all(%q(email ~* '21cn\.com$'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With