简体   繁体   English

将Rails ActiveRecord对象保存到临时表(MySQL)

[英]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). 数据通常包含数百个项目(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? 我们可以使用Rails / MySQL的临时Items表,如果是,我们应该怎么做?

You can use AR Extensions gem for this. 您可以使用AR Extensions gem。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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