我正在尝试删除更多,而不仅仅是一个 ID。我目前正在使用 Person.find(1).destroy
。
有没有一种方法可以让我选择多个数据记录?
最佳答案
是的。您可以在控制台中编写脚本。
Person.find_each do |person|
if # condition for deleting
person.destroy
end
end
或者,如果您知道所有 ID...您可以使用 where 子句然后销毁所有 ID。
ids = [1,2,3,4,5]
people = Person.where(id: ids) # where can take an array of ids
people.each { |person| person.destroy }
关于ruby-on-rails - Rails 控制台困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35997527/