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
CLICK HERE to find out more related problems solutions.