2

I'm importing legacy data into a new application, and there are duplicate entries for users.

I need to clean up this data (remove the duplicates), but I'm worried that those user ids may be referenced in other places.

I was hoping to write something in the model to catch any not found exceptions and return the correct id for the record in the event that one of the deleted (duplicate) ids are referenced.

Everything I'm seeing in google is a rescue used in conjunction with a Find statement in controllers. I don't want to have to litter my code with rescue statements in every single find statement I write.

Is there a way to rescue from ActiveRecord::RecordNotFound in the model? I'd need the offending ID in the method for reference.

Greg Blass
  • 3,523
  • 29
  • 42
  • Why not considering application controller as the _one source of truth_ to handle this exception like [this](http://stackoverflow.com/a/8783219/2767755) – Arup Rakshit Aug 07 '16 at 18:42
  • Personally, instead of patching the problem like you are trying to do I would clean up the data properly. Can you check which users have duplicate entries, then check every record in the db for that user_id and change it to the correct user_id? I know this sounds complicated but it might not be as complicated as it sounds depending on the size of your db, number of tables etc. I would personally fix the data instead of patching it like you are doing. Even more, why are there duplicate users? You should also make sure this does not happen again. – rii Aug 08 '16 at 20:06
  • In any case, if you did decide to go with your solution of rescuing, take a look at this answer: http://stackoverflow.com/a/13434571/664675 – rii Aug 08 '16 at 20:13
  • So the data set is fairly large. It'd be really tough to fix it all manually. – Greg Blass Aug 09 '16 at 02:19

0 Answers0