Zephyr4434
Zephyr4434

Reputation: 796

How do you pass an array into a model in a rails seed

I have a pretty simple question:

Let's say I am trying to create a new city in a City model in the db:seed file.

I have the following code in the seeds.rb and I want to pass in multiple values into an attribute for that city's sports teams like this:

City.create!(city: "Chicago,IL", teams: ["Bulls", "Cubs", "Bears"])

However, when I run the console and do City.first, I get the following:

#<City id: 375, created_at: "2013-04-05 02:55:32", updated_at: "2013-04-05 02:55:32", city: "Chicago,IL", teams: "---\n- Bulls\n- Cubs\n- Bears\n-">

Where are all those weird characters coming from in this result? Why does this not look like the array I was intending? I have tried a number of different approaches but none has made it work like I want.

How can I successfully pass an array into this attribute?

Upvotes: 0

Views: 926

Answers (1)

jvnill
jvnill

Reputation: 29599

You need to tell rails to serialize the attribute first. You can do that by adding the following code in your model

class City < ActiveRecord::Base
  serialize :teams, Array
  ...

Upvotes: 1

Related Questions