如果我有这样的参数:
params["scholarship"] = {"name"=>"test", "state_ids"=>["1", "2", "3", "4"]}
当我创建对象到数据库字段 state_id 时没有保存到数据库?
如何以格式保存到数据库:
#<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"] }
关于ruby-on-rails - 如何在rails中将数组保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23340222/