插入多个记录在同一时间进入模型

编程入门 行业动态 更新时间:2024-10-11 17:25:56
本文介绍了插入多个记录在同一时间进入模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我要寻找的一些最佳实践,当涉及到将数据插入一个模型,尤其是当有很多的记录被创建,至今我取回一些XML并将其保存到模型

I am looking for some best practices when it comes to inserting data into a model, especially when there are a lot of records to be created, so far I am retrieving some XML and saving it into a model

doc = Nokogiri::XML.parse(open(url)) doc.xpath('//xmlns:feed/xmlns:entry[xmlns:title[node()]]').each do |s| cid = s.xpath("xmlns:id").text email = s.xpath("gd:email/@address").text name = s.xpath("xmlns:title").text data = Contact.new( :cid => cid, :email => email, :name => name) data.save end

现在,这是一个它花费的时间太长,我认为插入一个记录。

Now this is inserting the records in one by one which is taking too long in my opinion.

我已阅读,一个解决方法是使用交易或我可以做一个单一的质量插入?我的问题是哪一个,我会获得最大收益以及如何将我重新格式化我已经拥有了所有这些?看到我的当前设置的一个实例到新的设置将有利于我,我能理解它更多从实际学习

I have read that one solution is to use transactions or I could do a single mass insert? My question is which one would I benefit the most from and how would i re format what i already have to each of these? Seeing an example of my current setup into the new setup would benefit me as I would be able to understand it more and actually learn from it

pciated任何帮助AP $ P $

Any help appreciated

感谢

推荐答案

别人已经解决了这一点,而解决方案是使用ActiveRecord的进口。请参见原来的问题 ...

Someone else has tackled this, and the solution was to use activerecord-import. See original question...

是它使用 ActiveRecord的导入详细的的维基

编辑:显然,链接按钮将不会复制链路作为标题,如果你不突出显示文本

Apparently the link button won't duplicate the link as title if you don't highlight text.

doc = Nokogiri::XML.parse(open(url)) data = [] doc.xpath('//xmlns:feed/xmlns:entry[xmlns:title[node()]]').each do |s| cid = s.xpath("xmlns:id").text email = s.xpath("gd:email/@address").text name = s.xpath("xmlns:title").text data << Contact.new( :cid => cid, :email => email, :name => name) end Contact.import data

更多推荐

插入多个记录在同一时间进入模型

本文发布于:2023-11-24 00:29:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623319.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   模型   时间   在同一

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!