序列化和反序列化ActiveRecord模型与孩子 [英] Serialize and deserialize ActiveRecord model with children

查看:229
本文介绍了序列化和反序列化ActiveRecord模型与孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表格有许多条目。序列化的形式和它的作品我用:

Form has many entries. To serialize form and it's entries I use:

json = @form.to_json( { :only => Form.accessible_attributes.to_a, :include => {:entries => {:only => Entry.accessible_attributes.to_a}}})

表和条目属性进行修改或删除,而他们在JSON。

Form and entries attributes can be modified or deleted while they are in JSON.

要反序列化我使用(但不工作):

To deserialize I use (but not working) :

@form = @form.from_json(json)
@form.save

如何保存条目在一次与形式?

How to save entries at once with form?

推荐答案

只好项改为entries_attributes:

Had to change entries to entries_attributes:

json = @form.to_json( { :only => Form.accessible_attributes.to_a, :include => {:entries => {:only => Entry.accessible_attributes.to_a}}}).gsub('"entries":[{', '"entries_attributes":[{')

这篇关于序列化和反序列化ActiveRecord模型与孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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