如何保存在轨阵列数据库 [英] how to save array to database in rails

查看:117
本文介绍了如何保存在轨阵列数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的PARAMS:

if i have params like this :

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 :state_ids
end

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

这篇关于如何保存在轨阵列数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆