Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在阵列上运行destroy_all?_Ruby On Rails_Postgresql_Activerecord_Rails Activerecord - Fatal编程技术网

Ruby on rails 在阵列上运行destroy_all?

Ruby on rails 在阵列上运行destroy_all?,ruby-on-rails,postgresql,activerecord,rails-activerecord,Ruby On Rails,Postgresql,Activerecord,Rails Activerecord,如何在阵列上运行destroy\u all 我有以下问题: spam_users = User.find_by_sql("SELECT * FROM users WHERE email ~* '21cn.com'") 我尝试运行了spam\u用户。destroy\u all,但我得到了未定义的方法“destroy\u all” 我在这个特定的应用程序中与PostgreSQL一起运行Rails 2.3.8。如果不在阵列上执行,destroy\u all是模型上的一种类方法。以下内容将消除垃圾邮件

如何在阵列上运行
destroy\u all

我有以下问题:

spam_users = User.find_by_sql("SELECT * FROM users WHERE email ~* '21cn.com'")
我尝试运行了
spam\u用户。destroy\u all
,但我得到了
未定义的方法“destroy\u all”

我在这个特定的应用程序中与PostgreSQL一起运行Rails 2.3.8。

如果不在阵列上执行,
destroy\u all
是模型上的一种类方法。以下内容将消除
垃圾邮件\u用户
数组中的所有内容:

User.destroy_all("email ~* '21cn.com'")
您还可以迭代
垃圾邮件\u用户
,并将其逐个销毁(如果您已经将其用于其他目的):

spam_users.each(&:destroy)
您可能还需要稍微调整一下正则表达式:

User.destroy_all("email ~* '21cn\\.com$'")
因此,您要查找的是文本
,而不是“任何字符”,并将其锚定到字符串的末尾。您还可以使用带引号的字符串
%q(…)
来减少转义:

User.destroy_all(%q(email ~* '21cn\.com$'))