Class: CsvDb

Inherits:
Object
  • Object
show all
Defined in:
app/models/csv_db.rb

Class Method Summary collapse

Class Method Details

.convert_save(model_name, csv_data, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
# File 'app/models/csv_db.rb', line 4

def convert_save(model_name, csv_data, &block)
  csv_file = csv_data.read
  target_model = model_name.pluralize.classify.constantize
  CSV.parse(csv_file, :headers => true, header_converters: :symbol ) do |row|
    if(block_given?)
        block.call(target_model, row.to_hash)
     else
         target_model.create!(row.to_hash)
     end
  end
end