title: How to Import XML Data in Rails 5
body: <p>I have xml data that I am trying to import my database. I am attempting to use the nokogiri gem. I have written a file called xml_data.rb. When I run <code>&gt;ruby xml_data.rb</code> on the command line, I recieve the following error:</p>
  
  <blockquote>
    <p>xml_data.rb:8:in <code>block in &lt;main&gt;': uninitialized constant Job (NameError)
            from C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/nokogiri-1.7.0.1-x86-mingw32/lib/nokogiri/xml/node_set.rb:187:in</code>block in each'
            from C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/nokogiri-1.7.0.1-x86-mingw32/lib/nokogiri/xml/node_set.rb:186:in <code>upto'
            from C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/nokogiri-1.7.0.1-x86-mingw32/lib/nokogiri/xml/node_set.rb:186:in</code>each'
            from xml_data.rb:7:in `'</p>
  </blockquote>
  
  <p>I'm not sure I understand how to do this properly. Below is the xml_data.rb file:</p>
  
  <pre><code>require 'nokogiri'
  
  OLD_DATA = 'data/old_data.xml'
  
  doc = File.open(OLD_DATA) {|f| Nokogiri::XML(f)}
  
  doc.css('request').each do |node|
      Job.create(
          :last_name =&gt; node['name'],
          :telephone =&gt; node['phone'],
          :street_address =&gt; node['address'],
          :city =&gt; node['city'],
          :state =&gt; node['state'],
          :zip =&gt; node['zip'],
          :email =&gt; node['email'],
          :au_chog =&gt; node['chogAu'],
          :person_type =&gt; node['affil'],
          :research_use =&gt; node['use'],
          :subject =&gt; node['subject'],
          :notes =&gt; node['note'],
          :start_date =&gt; node['startDate'],
          :end_date =&gt; node['addDate'],
          :complete =&gt; true,
          :time_spend =&gt; node['hours']
      )
  end
  </code></pre>
  
  <p>Ruby: v2.3.3
  Rails: v5.0.1</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I rewrote this code to be a rake task. However, I've not done this. When I run the task I get several syntax errors that have to do with the XML syntax, which is correct. Below is the updated code:</p>
  
  <pre><code>require 'nokogiri'
  
  namespace :db do
    namespace :seed do
  
      desc 'Add old data to database'
      task :import_data =&gt; :environment do
  
  
          OLD_DATA = "#{Rails.root}/lib/assets/data/old_data.xml"
  
          doc = Nokogiri::XML(File.open(OLD_DATA)) do |config|
              config.options = Nokogiri::XML::ParseOptions::STRICT
          end
  
          doc.css("request").each do |node|
              Job.create(
                  :last_name =&gt; node['name'],
                  :telephone =&gt; node['phone'],
                  :street_address =&gt; node['address'],
                  :city =&gt; node['city'],
                  :state =&gt; node['state'],
                  :zip =&gt; node['zip'],
                  :email =&gt; node['email'],
                  :au_chog =&gt; node["chogAu"],
                  :person_type =&gt; node['affil'],
                  :research_use =&gt; node['use'],
                  :subject =&gt; node['subject'],
                  :notes =&gt; node['note'],
                  :start_date =&gt; node['startDate'],
                  :end_date =&gt; node['addDate'],
                  :complete =&gt; true,
                  :time_spend =&gt; node['hours']
              )
          end
      end
    end
  end
  </code></pre>