使用 ar 扩展'导入:同步不起作用
我正在使用 AR-Extensions 将大量对象导入到数据库,但从数据库同步它们却不起作用。
我的代码:
posts = [Post.new(:name=>"kuku1"), Post.new(:name=>"kuku2"), ...]
Post.import posts, :synchronize=>posts
帖子提交到数据库,每个帖子都会自动分配主键(id)。但是当之后检查 posts 数组中的对象时,我发现它们没有 id 字段,并且 new_record 标志仍然为 true。 我还尝试添加 :reload=>true,但这也没有帮助。
知道为什么同步不起作用吗?
I am using AR-Extensions to import a large number of objects to db, but synching them back from DB just isn't working.
MY code:
posts = [Post.new(:name=>"kuku1"), Post.new(:name=>"kuku2"), ...]
Post.import posts, :synchronize=>posts
posts are submitted to db, and each one is allocated with primary key (id) automatically. But when afterwards checking the objects in posts array, I see that they don't have id field, and new_record flag is still true.
I also tried adding :reload=>true, but that doesn't help as well.
Any idea why synch doesn't work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于新记录来说,目前这是不可能的。从 ar-extensions 0.9.3 开始,同步新记录时这将不起作用,因为同步预计您要同步的记录已经存在。它在幕后使用主键来确定要加载的内容(但对于新记录,主键为零)。此限制*也存在于 activerecord-import 0.2.5 中。如果您可以在其他条件下同步,我很乐意发布一个新的 gem,允许传入条件。对于 Rails 3.x,您需要使用 activerecord-import (它取代了 ar-extensions)。请在 github 上创建票证/问题: https://github.com/zdennis/activerecord-import/issues
对于 Rails 2.x,您仍然想使用 ar-extensions,我可能会向后移植 activerecord-import 更新并推出一个新的 gem。如果您想在此处使用此功能,请在 github 上创建票证/问题:https://github.com /zdennis/ar-extensions/
也欢迎补丁。
*这里的限制是数据库约束,因为在一次插入/导入之后,如果不执行诸如表锁定之类的奇怪操作,就不可能获取所有新创建记录的 id,我认为这不是一个很好的解决方案问题。如果有人有想法,我会洗耳恭听。
更新
activerecord-import 0.2.6 和 ar-extensions 0.9.4 已发布,并支持指定要同步的字段。这些字段应该是唯一的。请参阅 http://www.continuousthinking.com/2011/4/6/activerecord-import-0-2-6-and-ar-extensions-0-9-4
This is not possible right now with new records. As of ar-extensions 0.9.3 this will not work when synchronizing new records as synchronizing expects the records you're sync'ing to already exist. It uses the primary key under the covers to determine what to load (but with new records the primary key is nil). This limitation* also exists in activerecord-import 0.2.5. If you can synchronize on other conditions I'd be happy to release a new gem allowing conditions to be passed in. For Rails 3.x you need to use activerecord-import though (it replaces ar-extensions). Please create ticket/issue on github: https://github.com/zdennis/activerecord-import/issues
For Rails 2.x you still want to use ar-extensions, and I'd likely backport the activerecord-import update and push out a new gem as well. If you'd like this functionality here please create a ticket/issue on github: https://github.com/zdennis/ar-extensions/
Patches are welcome as well.
*The limitation here is a database constraint, as its impossible to get the ids of all newly created records after a single insert/import without doing something strange like table locking, which I don't think is a good solution to that problem. If anyone has ideas I'm all ears.
UPDATE
activerecord-import 0.2.6 and ar-extensions 0.9.4 have been released and includes support for specifying the fields you want to synchronize on. Those fields should be unique. See http://www.continuousthinking.com/2011/4/6/activerecord-import-0-2-6-and-ar-extensions-0-9-4