Reputation: 163
I have a database that was populated using a rails system. I am trying to go back and delete a list of id's from one of the tables. I am currently deleting a single entry using the following command.
Item.find(1).destroy
I would like to be able to delete a series of id's with a single command. (ie. 1,6,5,8,12,14)
Item.find(1,6,5,8,12,14).destroy
I know that this doesn't work but thats the idea of what I'd like to do. I am fairly new to ruby and just trying to determine the simplest way to proceed.
Upvotes: 0
Views: 314
Reputation: 14412
No need to fetch the Items manually when Rails can do it for you:
Item.destroy_all(id: [1, 6, 5, 8, 12, 14])
or, as @ShankyMunjal posted, if you can use just ids, you can go with:
Item.destroy([1, 6, 5, 8, 12, 14])
Upvotes: 3
Reputation: 230461
This should work:
[1,6,5,8,12,14].each {|iid| Item.find(iid).destroy }
Upvotes: 1