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

  • Home
  • SEARCH
  • 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 8620807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:40:09+00:00 2026-06-12T06:40:09+00:00

I’m trying to read some values into an array in an .each do code

  • 0

I’m trying to read some values into an array in an .each do code block the simplest way possible but I’m having issues. I’m importing a csv file via a rake file I built, which will update a table with new rows, thus creating new id’s. I’m trying to take each id that is created and save them into an array. And I’m wanting to be able to access the values stored in that array in another rake task.

The following code is for the rake file that will import the csv file and whose newly generated id’s I wish to capture in an array. It currently does what it’s supposed to do but you can only import one new row at a time.

Import_incidents_csv.rake

require 'csv'

namespace :import_incidents_csv do

task :create_incidents => :environment do
    puts "Import Incidents"

    csv_text = File.read('c:/rails/thumb/costrecovery_csv/lib/csv_import/incidents.csv')
    csv = CSV.parse(csv_text, :headers => true)
    csv.each do |row|
        row = row.to_hash.with_indifferent_access
        Incident.create!(row.to_hash.symbolize_keys)
    end

    @last_incident_id = Incident.last.id

    end
  end

This is another rake file that imports another csv file that I need to have the values stored in the array assigned to. And again, currently it works fine if you just import one new row, but if you import multiple rows then everything goes a little haywire.

Import_timesheets_csv.rake

require 'csv'

namespace :import_timesheets_csv do

 task :create_timesheets => :environment do
    puts "Import Timesheets"

    csv_text = File.read('c:/rails/thumb/costrecovery_csv/lib/csv_import/timesheets.csv')
    csv = CSV.parse(csv_text, :headers => true)
    csv.each do |row|
        row = row.to_hash.with_indifferent_access
        Timesheet.create!(row.to_hash.symbolize_keys)
    end

    timesheet = Timesheet.last
    timesheet.incident_id = @last_incident_id
    timesheet.save

    @last_timesheet_id = Timesheet.last.id

     end
end

I read this resource for dealing with arrays http://www.tutorialspoint.com/ruby/ruby_arrays.htm and it seems very confusing. Here is my best guess of what the Import_incidents_csv.rake file might look like with reading values into an array. And I have the puts at the end so I can verify that the integers are properly getting stored in the array. Once I get everything working, I’ll remove it.

require 'csv'

def inc_id
@inc_id = Incident.last.id
end

namespace :import_incidents_csv do

 task :create_incidents => :environment do
    puts "Import Incidents"

    csv_text = File.read('c:/rails/thumb/costrecovery_csv/lib/csv_import/incidents.csv')
    csv = CSV.parse(csv_text, :headers => true)
    csv.each do |row|
        row = row.to_hash.with_indifferent_access
        Incident.create!(row.to_hash.symbolize_keys)
        Incident.last.id = @inc_id
        id_array = Array.new(@inc_id)
    end

    puts "#{id_array}"

      end
  end
  • 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-12T06:40:11+00:00Added an answer on June 12, 2026 at 6:40 am

    I’m not sure what you’re trying to do here, your question is far from clear, but it might be that you’re trying to collect the id values of the inserted records into an array:

    id_array = [ ]
    
    csv.each do |row|
      incident = Incident.create!(row.to_hash.symbolize_keys)
      id_array << incident.id
    end
    

    Once this has finished, id_array will have all of the created record id values in it. As a note, if any of these create! calls fails you will get an ActiveRecord::RecordInvalid exception which you will need to rescue somehow and handle. As such, it makes sense to wrap this entire operation inside of Indcident.transaction do ... end to ensure the whole thing can be rolled back if one fails, presuming your database supports transactions. If you don’t care about failures, you may call create instead which will not throw exceptions.

    There’s a lot of redundancy in your example there which has been omitted. Calling Array.new is hardly ever required as, like JavaScript, you can declare a new array with [ ] either empty or pre-populated with something like [ 1 ] or [ 1, 2 ].

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I have a French site that I want to parse, but am running into
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,

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.