Jump to content

The ultimate community for Ruby on Rails developers.


Rails Export XML, tell database

xml update database overwrite

  • Please log in to reply
1 reply to this topic

#1 shanek6



  • Members
  • 1 posts

Posted 31 July 2014 - 02:53 PM

Hi everyone, I've hit a bit of a wall with this.

I'm creating an e-commerce site and I've figured out how to export an xml of the orders table, this is just the top section of the file. This exports the file but every time I run the script it overwrites the last one. So first of all I need to find out how to numerically add a new file every time the script is run. like 1.xml, 2.xml


require 'builder'
@order = Spree::Order.all.limit(5)
  file = File.new("#{Rails.root}/public/orders.xml", 'w')
  xml = Builder::XmlMarkup.new(target: file, :indent => 4)
  xml.instruct! :xml, :version=>'1.0'
    @order.each do |o|
      xml.Order do
        xml.OrderReferences do
          xml.BuyersOrderNumber o.id
          xml.PORef "test"
        xml.OrderDate o.id
        xml.OrderTotal do
          xml.GoodsValue o.id
          xml.GoodsTax o.id
          xml.TaxTotal o.id
          xml.AmountPaid o.id
In the future there could be thousands of orders and I don't want every one of them to be scanned and exported every time the command is entered, I was of thinking a workaround could be to add a exported boolean field to the orders table but to be honest I'm a bit lost as to how to go about this. I'd really appreciate if anybody could give me a hand if they have any ideas.

  • shanek6 likes this

#2 Ohm



  • Moderators
  • 529 posts
  • LocationCopenhagen

Posted 31 July 2014 - 09:39 PM

The easiest way would be to just timestamp the files replacing 

file = File.new("#{Rails.root}/public/orders.xml", 'w')

with e.g.

file = File.new("#{Rails.root}/public/orders-#{Time.now.to_i}.xml", 'w')

This will give you order files like orders-1406842730.xml

Blog: http://ohm.sh | Twitter: @madsohm | Work: Lokalebasen.dk

Also tagged with one or more of these keywords: xml, update database, overwrite

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users