I search to load some csv file in my database, I'm using postgres. For this I got this method in my model
class Station < ApplicationRecord
def self.import_from_csv()
CSV.foreach('resources/locations.csv', headers: true) do|row|
station = Station.new(
id: row[0].to_i,
name: row['Name'],
city: row['City'],
default_dest: row['Default dest'],
public: row['Public'] == 'true',
lat: row['Lat'].to_f,
long: row['Long'].to_f
).save!
end
end
end
And this in my controller
def index
respond_to do |format|
format.html {
@stations = Station.all
render
}
format.json {
@total = Commute.count
@commutes = Commute.filter(params)
render
}
end
end
and this in my schema.rb
create_table "stations", force: :cascade do |t|
t.string "name", null: false
t.string "city", null: false
t.float "lat", null: false
t.float "long", null: false
t.boolean "public", null: false
t.string "default_dest"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
But no data is loaded into my db.
Does someone know if there is a rake command that I have to run or something like this?