ruby - How do you pass an array into a model in a rails seed -
i have pretty simple question:
let's trying create new city in city model in db:seed file.
i have following code in seeds.rb , want pass in multiple values attribute city's sports teams this:
city.create!(city: "chicago,il", teams: ["bulls", "cubs", "bears"])
however, when run console , city.first, 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 weird characters coming in result? why not array intending? have tried number of different approaches none has made work want.
how can pass array attribute?
you need tell rails serialize attribute first. can adding following code in model
class city < activerecord::base serialize :teams, array ...
Comments
Post a Comment