3

I'm trying to delete more that just one id. I'm currently using Person.find(1).destroy.

Is there a way that I can select more than just one data record?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Bitwise
  • 8,021
  • 22
  • 70
  • 161

1 Answers1

5

Yes. You can write scripts in the console.

Person.find_each do |person|
  if # condition for deleting
    person.destroy
  end
end

Alternatively, if you know all the ids...you can use a where clause and then destroy all of them.

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

people = Person.where(id: ids) # where can take an array of ids
people.each { |person| person.destroy }
toddmetheny
  • 4,405
  • 1
  • 22
  • 39
  • 5
    You can also call `Person.where(id: [1,2,3]).destroy_all` and `delete_all` (see http://stackoverflow.com/questions/6698207/delete-all-vs-destroy-all for more info) – MrYoshiji Mar 14 '16 at 20:54