Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 种子-创建或保存不工作_Ruby On Rails_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 种子-创建或保存不工作

Ruby on rails 种子-创建或保存不工作,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,这确实有效: result.each do |f| rt=ResTypes.new #( :type=>f['type'] ) rt.type=f['type'] rt.save() end 这不起作用: result.each do |f| ResTypes.create( :type => f['type'] ) end 这也不是: result.each do |f| rt=ResTypes.new( :type => f['type'] )

这确实有效:

result.each do |f|
  rt=ResTypes.new #( :type=>f['type'] )
  rt.type=f['type']
  rt.save()
end
这不起作用:

result.each do |f|
  ResTypes.create( :type => f['type'] )
end
这也不是:

result.each do |f|
  rt=ResTypes.new( :type => f['type'] )
  rt.save()
end
为什么第二条或第三条路不行

thx

编辑#1

这确实有效:

t=IO.read('db/seed-data/list-types.json')
lt_results=JSON.parse(t)
lt_results.each do |i|
  l=ListType.create(i)
end

可能与该属性无法进行质量分配有关。除此之外,它看起来应该可以工作。

我可以确认Maletor的建议,默认情况下,
类型
属性不可大量分配。这是因为它被设置为活动记录中的
继承\列
,用于单表继承。该字段通常由STI自动设置

要使其成为可以指定的对象,请在模型中设置:

class ResTypes < ActiveRecord::Base
  self.inheritance_column = nil
end
类重新类型
没错,我猜我用一个数组创建的其他种子看起来像编辑。这样行吗?