Elimiz de kullanıcıların bulunduğu bir tablo var ve biz bunu csv ile içe aktarmak istiyoruz. İlk olarak gem’i yüklememiz gerekiyor.
sessions/index.html.erb 
 | 1
2
3
4
5
6
 | <h2>Import Users</h2>
<%= form_tag import_sessions_path, multipart: true do %>
  <%= file_field_tag :file %>
  <%= submit_tag "Import" %>
<% end %>
 | 
 config/routes.rb 
 | 1
2
3
4
 | resources :sessions do
	collection { post :import }
end
	
 | 
 config/application.rb 
 | 1
2
 | require 'csv'
require 'iconv'
 | 
 controllers/sessions_controller.rb 
 | 1
2
3
4
5
 | def import
  User.import(params[:file])
  redirect_to root_url, notice: "Products imported."
end
	
 | 
 models/user.rb 
 | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 | def self.import(file)
  spreadsheet = open_spreadsheet(file)
  header = spreadsheet.row(1)
  (2..spreadsheet.last_row).each do |i|
	row = Hash[[header, spreadsheet.row(i)].transpose]
	product = find_by_id(row["id"]) || new
	product.attributes = row.to_hash.slice(*accessible_attributes)
	product.save!
  end
end
def self.open_spreadsheet(file)
  case File.extname(file.original_filename)
  when ".csv" then Csv.new(file.path, nil, :ignore)
  when ".xls" then Excel.new(file.path, nil, :ignore)
  when ".xlsx" then Excelx.new(file.path, nil, :ignore)
  else raise "Unknown file type: #{file.original_filename}"
  end
end
 | 
 Eğer yerelinizde çalışıyorsanız sisteme http://localhost:3000/sessions  ulaşabilirsiniz.
 DİKKAT !  
İmport edilen csv dosyasının içeriği, oluşturduğunuz kullanıcı tablosundaki özellikleri dikkate alınarak şekillenmelidir.