Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Let's say I have 2 models (Departments, Relations), I wanted import the data.csv to postgresql by creating a csv importer. I already created the tables (departments, relations) manually and set the attributes ahead. Currently, after much research, I could only import each model by creating a csv importer in each model respectively. I think this is not the most efficient way to import data if I have many model. I wanted to create a csv importer that can be used to import both models. Please guide me to move further. Below is my effort so far.

department.rb

class Department < ActiveRecord::Base
  def self.import(file)
    CSV.foreach(file.path, headers: true) do |row|
    Department.create! row.to_hash
   end
  end
end

relation.rb

class Relation < ActiveRecord::Base
  def self.import(file)
    CSV.foreach(file.path, headers: true) do |row|
    Relation.create! row.to_hash
   end
  end
end
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.