Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save Rails ActiveRecord objects into a temporary table (MySQL)

A user can import data into our website from a file. The data normally contains several hundred Items (Item < ActiveRecord::Base).

Although the validations help, they cannot solve the problem of sanity-checking the content. For that we would like to have a test mode.

Could we use a temporary Items table for this with Rails/MySQL, and, if yes, how should we do it?

like image 439
glebm Avatar asked Jan 20 '26 18:01

glebm


1 Answers

You can use AR Extensions gem for this. Read this 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
like image 165
Harish Shetty Avatar answered Jan 23 '26 08:01

Harish Shetty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!