我要寻找的一些最佳实践,当涉及到将数据插入一个模型,尤其是当有很多的记录被创建,至今我取回一些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更多推荐
插入多个记录在同一时间进入模型
发布评论