1

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?

1
  • any errors on your server logs ? Commented Aug 10, 2016 at 10:11

1 Answer 1

2

Updated answer for Rails 6.x

Rails now supports insert_all, so you don't need the activerecord-import gem.

def self.import_from_csv
  stations = CSV.foreach('resources/locations.csv', headers: true).map do |row|                                                                 
  { 
    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
  }
  end
  Station.insert_all(stations)
end

Old answer

You can hypotetically continue on this way and I would replace Station.new(...).save! with Station.create!(...).

You will soon face a problem in terms of time. If you are importing many records you approach will take reaaaally long. Please use activerecord-import gem and do that:

class Station < ApplicationRecord

  def self.import_from_csv
    stations = CSV.foreach('resources/locations.csv', headers: true).map do |row|                                                                 
      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
      )
    end
    Station.import(stations)
  end
end    
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you I do this with changing the new by create and using this gem and then I restart my server but nothing change...
Why this will be long ? This will be executed every time that I use the app or just the first time?
@maluss he meant it would be long if you stick with your initial approach. The method will only run when you call it
there is a way to call it in the rails console? I try this : Station.import_from_csv but it's tell me uninitialized constant Station::CSV
Did you add require 'csv' in config/application.rb?

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.