如何在rails中将数组保存到数据库

问题描述:

如果我有这样的参数:

params["scholarship"] = {"name"=>"test", "state_ids"=>["1", "2", "3", "4"]}

当我创建对象到数据库字段 state_id 时没有保存到数据库?

and when i create object to database field state_id not save to database?

如何以格式保存到数据库:

how to save to database with format :

#<Scholarship id: 1, name: "test", state_id: "["1", "2", "3", "4"]">

怎么做?

谢谢

ActiveRecord::Base.serialize.

例如:

class User < ActiveRecord::Base
  serialize :scholarship
end

user = User.create(:scholarship=> { "name" => "test", "state_ids" => ["1", "2"]})
User.find(user.id).scholarship# => { "name" => "test", "state_ids" => ["1", "2"] }