Rails控制台混淆

3
我正在尝试删除不止一个id。我目前使用的是Person.find(1).destroy
是否有一种方法可以选择多个数据记录?
1个回答

5
是的,你可以在控制台中编写脚本。
Person.find_each do |person|
  if # condition for deleting
    person.destroy
  end
end

另外,如果您知道所有的id ... 您可以使用where子句,然后销毁它们所有。

ids = [1,2,3,4,5]

people = Person.where(id: ids) # where can take an array of ids
people.each { |person| person.destroy }

5
您还可以调用Person.where(id: [1,2,3]).destroy_alldelete_all(有关更多信息,请参见https://dev59.com/IWw15IYBdhLWcg3wT59c) - MrYoshiji

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接