简体   繁体   中英

Rails 4: JSON backup back to Postgresql DB

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM