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