Eali
Eali

Reputation: 29

Rails - TypeError: can't cast Array to string

I'm trying to seed data into my database using rake db:seed. I get the following error TypeError: can't cast Array to string

My code

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

Looking through it, the syntax seems to be inorder. Can't seem to find why it's bringing up that error.

Can anyone help?

Upvotes: 0

Views: 1233

Answers (1)

Igal S.
Igal S.

Reputation: 14543

You are building an array of arrays, so location or university in the iterations are arrays of their own.

You don't need the extra [] around each one of the strings.

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

Upvotes: 3

Related Questions