Save Rails ActiveRecord objects into a temporary table (MySQL)
A user can import data into our website from a file. Th开发者_JS百科e 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?
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
精彩评论