我正在尝试使用rake db:seed
将数据播种到我的数据库中。我收到以下错误TypeError: can't cast Array to string
我的代码
db/seeds.rb
locations_list = [
["Melbourne"],
["Sydney"],
["Canberra"],
["Newcastle"]
]
locations_list.each do |location|
Location.create(city: location)
end
universities = [
["M University"],
["T University of M"],
["R Institute of M"],
["S University of T"],
["L University"],
["D University"],
["V University"]
]
universities.each do |university|
University.create(name: university)
end
通过它,语法似乎是有序的。似乎无法找到导致错误的原因。
有人可以帮忙吗?
答案 0 :(得分:1)
您正在构建一个数组数组,因此迭代中的location
或university
是它们自己的数组。
你不需要在每个字符串周围加上[]。
universities = [
"M University",
"T University of M",
"R Institute of M",
"S University of T",
"L University",
"D University",
"V University"
]