代码之家  ›  专栏  ›  技术社区  ›  glebm

将rails activerecord对象保存到临时表(mysql)中

  •  3
  • glebm  · 技术社区  · 16 年前

    用户可以将数据从文件导入我们的网站。 数据通常包含几百个项(item<activerecord::base)。

    尽管验证有帮助,但它们无法解决检查内容的健全性问题。 为此,我们希望有一个测试模式。

    我们可以在rails/mysql中使用一个临时的items表吗?如果可以,我们应该怎么做?

    1 回复  |  直到 16 年前
        1
  •  4
  •   Harish Shetty    16 年前

    你可以使用 AR Extensions 这是宝石。读这个 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
    
    推荐文章