I'm using a Ruby on Rails rake task to import a .csv file into my PostgreSQL database. My code, for reference, is below:
require 'csv'
namespace :db do
desc "Populate database"
task populate_database: :environment do
CSV.foreach("db.csv", :headers => true) do |row|
Sale.create!(row.to_hash)
end
end
end
In the .csv file, I have an entry in the format "cat; dog; foo; bar". Right now, this entry is being imported into PostgreSQL as "text" (i.e. the data type). However, I'd like for the text to be split at "; " and the results of the split to be stored in PostgreSQL as a text array.
How do I go about doing this?
EDIT FOR CLARIFICATION:
My data looks something like this:
column 1 | column 2 | column 3
row 1 John | Jack; Carol | Seth; Annie; Doug
row 2 Jim; Cassie; Jane | Anne | Laura; Bob
Right now, when I run my rake task, "John" goes into PostgreSQL as text. "Laura; Bob" also goes into PostgreSQL as text. However, I'd like for "Laura; Bob" (and, similarly, "Jack; Carol", "Seth; Annie; Doug", and "Jim; Cassie; Jane") to go into PostgreSQL as a text array. One such array would look like [Jack, Carol].