UnknownAttributeError csv

时间:2016-05-13 23:36:48

标签: ruby-on-rails csv

尝试将csv记录上传到City model数据库,收到错误

ActiveRecord::UnknownAttributeError in PagesController#import

unknown attribute 'country ' for City.  (City.create! row.to_hash)

CSV:

name     country    general_info1    general_info2 (etc)
Toronto  Canada     This is a test   (nil)

上传视图

<%= form_tag upload_path, multipart: true do %>
  <%= file_field_tag :file %>
  <%= submit_tag "import" %>
<% end %>

路线:

post '/upload' => 'pages#import'

PagesController

def import
    City.import(params[:file])
end

城市模型

def self.import(file)
    logger.info "__________________"
    logger.info file.inspect
    logger.info file.path
    logger.info "__________________"

    CSV.foreach(file.path, headers: true) do |row|
      City.create! row.to_hash
      #puts '&&&&&&&&&&&&&&&&&&&&&&&' + row[1]
      logger.info row.inspect
    end

模式

ActiveRecord::Schema.define(version: 20160513090837) do

  create_table "cities", force: :cascade do |t|
    t.string   "name"
    t.string   "country"
    t.string   "general_info1"
    t.string   "general_info2"
    t.integer  "happiness_rating"
    t.integer  "family_safety_rating"
    t.string   "family_safety_info"
    t.integer  "bike_hobby_rating"
    t.string   "bike_hobby_info"
    t.integer  "accountant_rating"
    t.integer  "accountant_shortage_rating"
    t.string   "accountant_shortage"
    t.integer  "accountant_avg_salary"
    t.integer  "graphic_designer_rating"
    t.string   "graphic_designer_shortage"
    t.integer  "graphic_designer_avg_salary"
    t.integer  "journalist_rating"
    t.string   "journalist_shortage"
    t.integer  "journalist_avg_salary"
    t.datetime "created_at",                  null: false
    t.datetime "updated_at",                  null: false
  end

end

非常感谢提前

答案:

CSV.foreach(file.path, {headers: true, header_converters: :symbol}) do |row|
        City.create!(row.to_hash)
      end

1 个答案:

答案 0 :(得分:0)

如果field表格中City create!方法预期出现在cities表格中的Hash缺席,则会发生keys 。确保它在那里。

  

有人可以解释为什么标题需要是一个符号吗?

在ruby中解析CSV时,通常会将数据作为Hash es的集合获取。在以下示例中,您可能已经注意到所有String的{​​{1}}正在重复,并且它们是Ruby数据。在String中,每个csv.to_a.map {|row| row.to_hash } # => [{"Year"=>"1997", "Make"=>"Ford", "Model"=>"E350", "Description"=>"ac, abs, moon", "Price"=>"3000.00"}, {"Year"=>"1999", "Make"=>"Chevy", "Model"=>"Venture \"Extended Edition\"", "Description"=>"", "Price"=>"4900.00"}, {"Year"=>"1999", "Make"=>"Chevy", "Model"=>"Venture \"Extended Edition, Very Large\"", "Description"=>nil, "Price"=>"5000.00"}, {"Year"=>"1996", "Make"=>"Jeep", "Model"=>"Grand Cherokee", "Description"=>"MUST SELL!\nair, moon roof, loaded", "Price"=>"4799.00"}] 实例占用不同的内存空间。

computer

假设字符串8 bytes占用1000,000。如果我定义此字符串8MB次,它将占用我的RAM的header_converters: :symbol

现在,如果您使用to_sym,则会调用keys方法,该方法现在将symbol定义为csv = CSV.new(body, :headers => true, :header_converters => :symbol) csv.to_a.map {|row| row.to_hash } # => [{:year=>"1997", :make=>"Ford", :model=>"E350", :description=>"ac, abs, moon", :price=>"3000.00"}, {:year=>"1999", :make=>"Chevy", :model=>"Venture \"Extended Edition\"", :description=>"", :price=>"4900.00"}, {:year=>"1999", :make=>"Chevy", :model=>"Venture \"Extended Edition, Very Large\"", :description=>nil, :price=>"5000.00"}, {:year=>"1996", :make=>"Jeep", :model=>"Grand Cherokee", :description=>"MUST SELL!\nair, moon roof, loaded", :price=>"4799.00"}]

:computer

符号有什么特别之处?

符号具有内存效率,检索速度非常快。如果您定义符号1000,000 8Bytes次,它只会占用Hash个内存。

这个解释如何解决我的问题?

您的CSV文件可能有数千行数据要转换为header_converters: :symbol。因此,为了使此过程更快,内存效率更高,您可以使用<Products>

有关详情http://ruby-doc.org/stdlib-2.0.0/libdoc/csv/rdoc/CSV.html#HeaderConverters

,请参阅此处