Rails - TypeError:无法将Array转换为字符串

时间:2015-05-24 06:26:47

标签: ruby-on-rails-4

我正在尝试使用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

通过它,语法似乎是有序的。似乎无法找到导致错误的原因。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

您正在构建一个数组数组,因此迭代中的locationuniversity是它们自己的数组。

你不需要在每个字符串周围加上[]。

universities = [
  "M University",
  "T University of M",
  "R Institute of M",
  "S University of T",
  "L University",
  "D University",
  "V University"
]