如何在rails中将数组保存到数据库 [英] how to save array to database in rails
本文介绍了如何在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"]">
怎么做?
谢谢
推荐答案
例如:
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"] }
这篇关于如何在rails中将数组保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文