我有一个很大的CSV文件,我想填充一个模型,一旦列填充CSV就不会改变。我也有我认为会采用CSV并将其添加到数据库模型的方法。我不确定在哪里放置该方法以及如何调用它?在哪里添加代码CSV到模型轨道
目前我在我的Rails应用程序中的.rb文件中有方法,我从终端调用它。但我收到以下错误:
ruby postcode.rb
postcode.rb:6:in `block in <main>': uninitialized constant Place (NameError)
from postcode.rb:5:in `each'
from postcode.rb:5:in `<main>'
Method/.rb file:
这是我解析CSV并将数组中的每个数组添加到'Place'模型的方法。
require 'csv'
csv_text = File.read('GB_full.csv')
csv = CSV.parse(csv_text, :headers => false)
csv.each do |row|
Place.create!(row.to_hash)
end
型号:
class CreatePlaces < ActiveRecord::Migration
def change
create_table :places do |t|
t.string :country_code
t.string :postal_code
t.string :place_name
t.string :admin_name_1
t.string :admin_code_1
t.string :admin_name_2
t.string :admin_code_2
t.string :admin_name_3
t.string :admin_code_3
t.string :latitude
t.string :longitude
t.string :accuracy
t.timestamps null: false
end
end
end
谢谢@MaxWilliams,但我不知道该怎么做。 – Stoob
然后看! –