0

I have two arrays, one with information from the old DB and one with information from the new DB. I want to check what is in the old DB that is NOT in the new DB, and delete that.

I have done array1 - array2 and put the results in an array, but am not sure how to then remove them from the DB. Any enum I have tried to call on it seems to break/not like being used with a fixnum.

Using ruby 1.8.7 running on an oracle DB, in case there is an easier way to do it inside oracle (first time using oracle so I am unfamiliar).

Mark Thomas
  • 37,131
  • 11
  • 74
  • 101
Noahhh
  • 15
  • 6

1 Answers1

0

If you use AR ORM, you could do:

(array1 - array2).each(&:destroy)

of you could simply delete_all:

(array1 - array2).delete_all

EDIT based on comment:

Since array1 and array2 are arrays of ids, you could simply pass resulting array into Merchant.where clause and delete the collection all together:

ids_for_delete = (array1 - array2)
Merchant.find(ids_for_delete).delete_all

Sidenote: maintenance of Ruby versions 1.8.7 and 1.9.2 ended on July 31, 2014 - consider update.

Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
  • I guess I should have mentioned, as I keep getting 'undefined method `destroy' for Fixnum', that I am trying to delete integers (ex. array1 = Merchant.find(:all).map{|m| m.id} to get the ids). – Noahhh May 14 '15 at 20:28
  • I got it working with Merchant.find(ids_for_delete).delete_all I'm not sure 1.8.7 has where...? It wasn't liking it for me, at least. Thank you for pointing me in the right direction! – Noahhh May 14 '15 at 20:38
  • @Noahhh I can update the answer with the working code (I'm not familiar with Ruby 1.8.7) and you could accept it if it helped – Andrey Deineko May 14 '15 at 20:40
  • Done, thank you! I am unfamiliar with 1.8.7 as well, which is why I wasn't sure what I was missing. – Noahhh May 14 '15 at 20:45