Ruby on rails 如何在RubyonRails中为种子文件分配现有ID?

Ruby on rails 如何在RubyonRails中为种子文件分配现有ID?,ruby-on-rails,Ruby On Rails,因此,我有一个文件,其中包含一组数据,我正试图将这些数据植入数据库,并将其分配给购物车。我已经创建了一个购物车,我只想把所有的东西都分配给一个单一的购物车。问题是试图找到我应该实际放置购物车id的位置。种子如下所示: require 'csv' csv_text = File.read(Rails.root.join('lib', 'seeds', 'IronGloryInventory.csv')) csv = CSV.parse(csv_text, :headers => tr

因此,我有一个文件,其中包含一组数据,我正试图将这些数据植入数据库,并将其分配给购物车。我已经创建了一个购物车,我只想把所有的东西都分配给一个单一的购物车。问题是试图找到我应该实际放置购物车id的位置。种子如下所示:

 require 'csv'

 csv_text = File.read(Rails.root.join('lib', 'seeds', 'IronGloryInventory.csv'))
 csv = CSV.parse(csv_text, :headers => true, :encoding => 'ISO-8859-1')
 csv.each do |row|
 @product = row.to_hash
 t = Product.new
 t.product_name = row['Product']
 t.sku = row['SKU']
 t.price = row['Price']
 t.year = row['Year']
 t.quantity = row['Available'].gsub(/\:|\D/, '')
 t.size = row['Available'].gsub(/\:|\d/, '')
 t.category = row['Category']
 t.description = row['Description']
 t.save
 puts "#{t.product_name}, #{t.year}, #{t.size}, #{t.category} "
end
我试着把t.cart_id=1放在t=Product.new下面,得到了一个命名错误

我试着把它放在csv.each do with product.cart_id=1上面,得到一个未定义的局部变量

我觉得实际上应该在迭代中分配id,但我无法理解


购物车与具有多个的产品关联,并且产品具有所属的。任何方向都会非常有用。

您的产品模型中没有
购物车id
属性,这就是
命名错误的原因

您可以创建相应的迁移来添加它,运行db:migrate将更改持久化到架构中,然后重试

$ rails g migration add_cart_to_products cart:references
$ rails db:migrate
product.cart\u id
赋值必须在
每个
块中,否则Rails将尝试查找名为product的局部变量,该变量不存在

require 'csv'

csv_text = File.read(Rails.root.join('lib', 'seeds', 'IronGloryInventory.csv'))
csv = CSV.parse(csv_text, headers: true, encoding: 'ISO-8859-1')
csv.each do |row|
  product = Product.new
  product.product_name = row['Product']
  product.sku = row['SKU']
  product.price = row['Price']
  product.year = row['Year']
  product.quantity = row['Available'].gsub(/\:|\D/, '')
  product.size = row['Available'].gsub(/\:|\d/, '')
  product.category = row['Category']
  product.description = row['Description']
  product.cart_id = 1
  product.save
  puts "#{product.product_name}, #{product.year}, #{product.size}, #{product.category} "
end

您的产品模型中没有
cart\u id
属性,这就是
NoMethodError
的原因

您可以创建相应的迁移来添加它,运行db:migrate将更改持久化到架构中,然后重试

$ rails g migration add_cart_to_products cart:references
$ rails db:migrate
product.cart\u id
赋值必须在
每个
块中,否则Rails将尝试查找名为product的局部变量,该变量不存在

require 'csv'

csv_text = File.read(Rails.root.join('lib', 'seeds', 'IronGloryInventory.csv'))
csv = CSV.parse(csv_text, headers: true, encoding: 'ISO-8859-1')
csv.each do |row|
  product = Product.new
  product.product_name = row['Product']
  product.sku = row['SKU']
  product.price = row['Price']
  product.year = row['Year']
  product.quantity = row['Available'].gsub(/\:|\D/, '')
  product.size = row['Available'].gsub(/\:|\d/, '')
  product.category = row['Category']
  product.description = row['Description']
  product.cart_id = 1
  product.save
  puts "#{product.product_name}, #{product.year}, #{product.size}, #{product.category} "
end

您的产品型号是否具有购物车id属性?你能添加你的模式文件吗?创建表“carts”,force::cascade do | t | t.boolean“purchase”t.datetime“created|u at”,null:false t.datetime“updated|at”,null:false t.integer“user|id”t.integer“guest|id”t.index[“guest|id”],name:“index|u carts|u on|guest|id”t.index[“product|id”],name:“index_carts_on_product_id”t.index[“user_id”],name:“index_carts_on_user_id”endcreate_table“products”,force::cascade do | t | t.string“product|name”t.string“sku”t.integer“price”t.integer“year”t.boolean“availability”t.integer“数量”t.string“size”t.text“description”t.integer“user|id”“t.integer”guest\u id“t.integer”category\u id“t.string”category”t.index[“category\u id”]、name:“index\u products\u on\u category\u id”t.index[“guest\u id”]、name:“index\u products\u on\u on\u user\u id”结束购物车和产品的架构。您的产品模型中没有
cart\u id
。尝试创建迁移,例如
rails g migration add\u cart\u to\u products cart:references
。您的产品模型是否具有cart\u id属性?你能添加你的模式文件吗?创建表“carts”,force::cascade do | t | t.boolean“purchase”t.datetime“created|u at”,null:false t.datetime“updated|at”,null:false t.integer“user|id”t.integer“guest|id”t.index[“guest|id”],name:“index|u carts|u on|guest|id”t.index[“product|id”],name:“index_carts_on_product_id”t.index[“user_id”],name:“index_carts_on_user_id”endcreate_table“products”,force::cascade do | t | t.string“product|name”t.string“sku”t.integer“price”t.integer“year”t.boolean“availability”t.integer“数量”t.string“size”t.text“description”t.integer“user|id”“t.integer”guest\u id“t.integer”category\u id“t.string”category”t.index[“category\u id”]、name:“index\u products\u on\u category\u id”t.index[“guest\u id”]、name:“index\u products\u on\u on\u user\u id”结束购物车和产品的架构。您的产品模型中没有
cart\u id
。尝试创建迁移,例如
railsg迁移将购物车添加到购物车:参考