0

I have an issue re-creating my app DB from JSON backup file.

What I am trying to do now is to create all the records from rake task reading lines in the file and rendering json like:

  new_record = key.camelize.constantize.new(hs['key'])
  new_record.save

The problem is that I can not skip callbacks for all the models, to be sure I am not creating any dup stuff there. .camelize.constantize.skip_callback(:after_create) is just not working giving me an error undefined method '_after_create_callbacks' for #<Class:.

So two questions here:

1) Is there any way to skip AR callbacks other way? 2) Are there any other options for re-creating db from JSON except SQL queries?

alv_721
  • 305
  • 3
  • 18

1 Answers1

1

1. Skipping callbacks

You can ask AR to skip validation callbacks when changing a record like this:

new_record.class.skip_callback(:create, :after, :specific_callback)
new_record.save

2. Importing JSON into PSQL

Since your data is in JSON, there is no magical way of importing it into your DB. But, Postgres allows you easily import it using SQL like it is shown in this SO answer. Also, many others have written utilities to make your life easier.

Community
  • 1
  • 1
Uzbekjon
  • 11,655
  • 3
  • 37
  • 54
  • Thanks, I will check second one more carefully :) What about first one - I do not need skipping validations, data in the dump is valid, I need to skip `after_create/after_save` callbacks to not re-create some of the connected records (which are already there in the dump). – alv_721 May 05 '16 at 09:11
  • Ahh... sorry, still waking up. Had a long night :) With the `skip_callback` I think you need to call it something like this: `skip_callback :create, :after`. Updated my answer. – Uzbekjon May 05 '16 at 09:18