insert-update - 如何在 Rhomobile 中对 create/update_attributes 的单次调用中插入/更新多条记录

标签 insert-update rhomobile rhodes multiple-records

根据 performance tip in Rhom API罗莫比尔,

我们应该首先准备整个数据集,然后调用 create/update_attributes,以获得比准备单个记录然后在循环内调用 create 更好的性能。

据我所知,create方法采用单个记录的对象,如下所示,

@account = Account.create(
  {"name" => "some new record", "industry" => "electronics"}
)

所以我想知道如何在一次调用中创建/更新多个记录?

提前致谢。

最佳答案

首先,我不知道这实际上会对性能产生多大影响,无论是积极还是消极,并且从未对其进行过测量。

也就是说,您可以将所有 CRUD 调用包装在一个事务中,以最大限度地减少打开和关闭的数据库连接。如果某些记录导致新数据集出现问题,这还可以通过回滚更改来帮助您维护引用完整性。

# Load all DB Models, to ensure they are available before first time import
Rho::RHO.load_all_sources();

# Get instance of DB to work transactions with
db = ::Rho::RHO.get_db_partitions()['local'] # Get reference to model db
db.start_transaction()  # BEGIN transaction

... Do all your create/update/deletes

if (was_import_successful)
  db.commit   # COMMIT transaction
else
  db.rollback() # ROLLBACK transaction
end

关于insert-update - 如何在 Rhomobile 中对 create/update_attributes 的单次调用中插入/更新多条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18336497/

相关文章:

ruby - 在 rhoMobile 中显示单击按钮的 View

jquery-mobile - jquery mobile 在转换后闪烁上一页

javascript - 处理 ruby​​ 中的正则表达式

iphone - iphone 中的 Rhodes 或 Titanium for Rails 应用程序?

php - oscommerce tep_db_perform -- 更新

r - 在某个键首次出现后,为当前没有数据的日期创建 0

php - 更新记录,收到成功消息,但数据库中的数据未更新

iphone - 在最新框架(Rhodes、PhoneGap、jQTouch)中开发跨平台移动应用程序

Ruby 时区在 Windows 7 中返回完整形式

Mysql在非空字段上插入或更新为空值时不会出错