簡體   English   中英

將Rails ActiveRecord對象保存到臨時表(MySQL)

[英]Save Rails ActiveRecord objects into a temporary table (MySQL)

用戶可以從文件中將數據導入我們的網站。 數據通常包含數百個項目(Item <ActiveRecord :: Base)。

盡管驗證有所幫助,但它們無法解決檢查內容的完整性問題。 為此,我們希望有一個測試模式。

我們可以使用Rails / MySQL的臨時Items表,如果是,我們應該怎么做?

您可以使用AR Extensions gem。 閱讀本文了解更多詳情。

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

要么

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