【问题标题】:Save Rails ActiveRecord objects into a temporary table (MySQL)将 Rails ActiveRecord 对象保存到临时表中 (MySQL)
【发布时间】:2010-03-09 15:49:58
【问题描述】:

用户可以将数据从文件导入我们的网站。 数据通常包含数百个项目 (Item

虽然验证有帮助,但它们无法解决对内容进行完整性检查的问题。 为此,我们希望有一个测试模式。

我们能否在 Rails/MySQL 中为此使用临时 Items 表,如果可以,我们应该怎么做?

【问题讨论】:

    标签: mysql ruby-on-rails activerecord temp-tables


    【解决方案1】:

    您可以为此使用AR Extensions gem。阅读此article 了解更多详情。

    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
    

    【讨论】:

      猜你喜欢
      • 2011-07-04
      • 2013-09-16
      • 2015-04-28
      • 1970-01-01
      • 2011-01-11
      • 2014-12-26
      • 1970-01-01
      • 1970-01-01
      • 2021-03-14
      相关资源
      最近更新 更多