I want to put rails CSV file in DB with seed.

Asked 2 years ago, Updated 2 years ago, 72 views

Creating an application with rails6.

When I created the data to be put into the DB in a CSV file so that the data could be put into the seed, an error occurred and I stumbled.
I would appreciate it if you could let me know.

Error Statements in Terminal

NameError:uninitialized constant Products
/Users/user/projects/medipra/db/seeds.rb:12:in`block in<main>'
/Users/user/projects/medipra/db/seeds.rb:11:in`<main>'
Tasks: TOP=>db:seed

Seeds.rb Code

require "csv"
date="2019-11-11"

CSV.foreach('db/y_ALL20191015.csv') do|info|
  Products.create(change_category:info[0], master_type:info[1], chemical_code:info[2], kanji_significant_digits:info[3], name:info[4], kana_significant_digits:info[5], kana_name:info[7], info[7]
    unit_name —info[9],
    price_type —info[10],
    price: info[11],
    spare_1—info[12],
    n_p_s_p_drags: info [13],
    nerve_destroyer: info[14],
    biologics: info[15],
    generic: info [16],
    spare_2: info[17],
    dental_drugs: info [18],
    contrast_agent:info[19],
    injection_volume: info[20],
    listing_type: info[21],
    product_name_relations —info[22],
    old_price_type: info[23],
    old_price —info[24],
    name_chnage_category: info[25],
    kana_name_change_category: info[26],
    damage_form —info[27],
    spare_3: info[28],
    changed_date: info [29],
    validation_date: info[30],
    standard_code —info[31],
    order_number —info[32],
    expiration_date: info [33],
    standard_name —info [34],
    created_at:date,
    updated_at:date
  )
e n d

The Products table has already been created in the DB and columns have been migrated.

Additional
db/schema.rb


ActiveRecord::Schema.define(version:2019_11_10_125708)do

  create_table "products", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", force: :cascade do | t |
    t. integer "change_category"
    t.string "master_type"
    t. integer "pharmaceutical_code"
    t. integer "kanji_significant_digits"
    t.string "name"
    t. integer "kana_significant_digits"
    t.string "kana_name"

There is a space after the name that doesn't make sense.
This is probably the reason.

ruby-on-rails csv

2022-09-30 21:44

1 Answers

As mentioned in the comments, the column does not fit well because of the space behind the name.
That was the result of the error.

I think the full-width space was set to stand out, but it didn't seem to work well.


2022-09-30 21:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.