Save Rails ActiveRecord objects to temporary table (MySQL)

User can import data to our website from a file. Typically, data contains several hundred elements (Item <ActiveRecord :: Base).

Although validations help, they cannot solve the problem of verifying the health of content. For this, we would like to have a test mode.

Is it possible to use a temporary table Items for this with Rails / MySQL and, if so, how to do it?

+3
source share
1 answer

You can use AR Extensions for this. Read this article article for more details.

User.create_temporary_table do | temp_model|
  # now perform the inserts on temp table.
  temp_model.create(...)
  ...
end # table dropped automatically 

OR

temp_model = User.create_temporary_table
temp_model.create(...)
#do something
...
...
#drop the temp table
temp_model.drop
+4

Source: https://habr.com/ru/post/1736140/


All Articles