Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Say for instance you wanted to parse all charities in the IRS txt file that is pipe delimited. </p> <p>Say you had a model called Charity that had all the same fields as your pipe delimited file.</p> <pre><code>class Charity &lt; ActiveRecord::Base # http://apps.irs.gov/app/eos/forwardToPub78DownloadLayout.do # http://apps.irs.gov/app/eos/forwardToPub78Download.do attr_accessible :city, :country, :deductibility_status, :deductibility_status_description, :ein, :legal_name, :state end </code></pre> <p>You can make a rake task called import.rake</p> <pre><code>namespace :import do desc "Import Pipe Delimted IRS 5013c Data " task :irs_data =&gt; :environment do require 'csv' txt_file_path = 'db/irs_5013cs.txt' results = File.open(txt_file_path).readlines do |line| line = line.split('|').each_slice(7) end # Order Field Notes # 1 EIN Required # 2 Legal Name Optional # 3 City Optional # 4 State Optional # 5 Deductibility Status Optional # 6 Country Optional - If Country is null, then Country is assumed to be United States # 7 Deductibility Status Description Optional results.each do |row| row = row.split('|').each_slice(7).to_a.first #ID,Category,Sub Category,State Standard Charity.create!({ :ein =&gt; row[0], :legal_name =&gt; row[1], :city =&gt; row[2], :state =&gt; row[3], :deductibility_status =&gt; row[4], :country =&gt; row[5], :deductibility_status_description =&gt; row[6] }) end end end </code></pre> <p>finally you can run this import by typing following on command line from your rails app </p> <pre><code> rake import:irs_data </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload