0

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?

1 Answer 1

3

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"
]
Sign up to request clarification or add additional context in comments.

1 Comment

I've just tried that and it works, thanks for the help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.