我可以在阵列上使用update_all吗?

kru*_*hah 7 ruby ruby-on-rails

我有一个数组中的注释列表.我可以在阵列上使用update_all吗?

comments = Comments.find(:all,:conditions => ["test is not null"]) 

comments.update_all(:test => nil)
Run Code Online (Sandbox Code Playgroud)

tok*_*and 10

您可以使用范围(findall-in旧版本的Rails-返回一个数组):

comments = Comments.scoped(:conditions => "test IS NOT NULL")
comments.update_all(:test => nil)
Run Code Online (Sandbox Code Playgroud)

在现代版本的Ruby/ActiveRecord上,您可以编写:

Comments.where.not(test: nil).update_all(test: nil)
Run Code Online (Sandbox Code Playgroud)