0

I am trying to import a whole bunch of csv files into a postgreSQL database. CSV files are format is shown below. I want to be able to swap between html files and display different statistics (tables) for each county.

County,Number,Statistic,Year,Source,
Atlantic;403;Assualt;2010;https://www.njsp.org/ucr/uniform-crime-reports.shtml
Bergen;529;
Burlington;267;
Camden;859;
Cape May;160;
Cumberland;300;
Essex;1275;
Glouchester;209;
Hudson;838;
Hunterdon;38;
Mercer;416;
Middlesex;519;
Monmouth;442;
Morris;202;
Ocean;322;
Passaic;852;
Salem;67;
Somerset;73;
Sussex;53;
Union;421;
Warren;453;

There a bunch of csv files where the statistic I'm tracking is different i.e. assault, theft, etc. Is there a way to import all of these csv files and create a table while doing that? Or would it be better to create a table inside the schema.rb file and then run some sort of script inside the seeds.rb file?

1 Answer 1

1

Rails convention suggest not to write raw sql, when it is possible using Rails ORM - ActiveRecord

Normally in Rails you should create migration where you define your tables structure and define ActiveRecord model with name matching your table like CountyStat

class CreateCountyStats < ActiveRecord::Migration[5.0]
  def change
    create_table :county_stats do |t|
      t.string :county, index: true
      t.integer :number
      t.string :statistic
      t.text :source

 
      t.timestamps
    end
  end
end

Then you will need to parse given CSV, and import into database. seeds.rb is used mostly once, during initial application setup, alternative ways you can wrap it inside rake task.

CSV.foreach(csv_path, headers: true) do |row|
  attributes = row.to_h.transform_keys(&:downcase)

  CountyStat.create(attributes)
end

Afterwards you can fetch your data by using ActiveRecord model. If you need to search by some column, you can add index (I already provided for county), also you can define scopes for your counties (if needed)

class CountyStat
  scope :bergen, -> { where(county: "Bergen" }
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.