Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9281485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:07:59+00:00 2026-06-18T18:07:59+00:00

I have a structured comma delimited file that has two record types. The different

  • 0

I have a structured comma delimited file that has two record types. The different records are differentiated by a header entry: H or P. The file format follows:

"H","USA","MD","20904"
"P","1","A","Female","W"
"P","2","A","Male","H"

I’d like to import the file and then create activerecord models with the imported data. The approach that I am using is to create a field map that includes the number of fields, object name and columns.

I then utilize the field map

$field_map =

{
  'H'   =>
    {
      :count => 4,
      :object => :Header,
      :cols => [:record_type, :country_id, :state, :zip]
    },

  'R'   =>
    {
      :count => 4,
      :object => :RaceData,
      :cols => [:record_type, :household_size, :gender, :race]
    }
}

I then use FastCSV to import the file and use a case statement to how the file will be transformed and then used in activerecord create statements.

FasterCSV.foreach(filename) do |row|
    tbl_type = row[0]
    tbl_info = $field_map[tbl_type]

   unless (tbl_info.nil?)
     field_no = tbl_info[:count]
     object = tbl_info[:object]
     columns = tbl_info[:cols]
     record_type = new_record[:record_type]
     case record_type
         when "H"
         factory_build_h_record(new_record)
         when "P"
         factory_build_p_record(new_record)
     end
   end
 end

The code above is summarized due to space constraints. My approach works just fine, but my I’m new to ruby and I’m always interested in best practices and the “true” Ruby-way of doing things. I’d be interested in hearing how more experienced programmers would tackle this problem. Thanks for your input.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T18:08:00+00:00Added an answer on June 18, 2026 at 6:08 pm

    I suggest the gem 'roo'

    You have an example source code here but I rather watch the 10 min video

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have comma deliminated file that basically has the structure of: 1, 2, 3,
So I have a plist structured string, that get dynamically (not from the file
I have a comma delimited text file with the structure field1 field2 field3 field4
I have a text file which has the following structure: 341|18 Hello world|20090225230048AAnhStI|90|$0.30|10|289|2|2|2|Is that
I have a highly structured hierarchical directory containing multiple files that need to be
I have a flat structured XML file as below: <rs> <r id=r1 lev=0/> <r
I have a structure, that i want to write to a regular file as
I have a large (~100GB) text file structured like this: A,foobar A,barfoo A,foobar B,barfoo
i have these two queries, that i want to join together, but am not
I have a file that contains parameters using this syntax RANGE {<value> | <value>-<value>}

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.