Rails 构建 N 条记录的序列化对象 [英] Rails building a Serialized object of N records

查看:42
本文介绍了Rails 构建 N 条记录的序列化对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想序列化文本列中的任何对象.

通过 API,我得到参数:

params[:attachments] -- 这可以是 0 或 1 或 3,或 100+ 等等...params[:attachment1]...params[:attachment2] ...params[:attachmentN]

那么如何在序列化对象中存储 X # 个附件?

mailThing = MailThing.create(:attachments => myAttachmentsSerizliedIfANY)

我正在尝试:

@myAttachmentsSerizliedIfANY = nil我 = 0attachmentCount = params[:attachments].to_i当我 <附件计数做@myAttachmentsSerizliedIfANY <<参数[:附件 + i]我 += 1结尾

有关如何使其工作的任何建议?谢谢

解决方案

好的,我看了你的其他一些问题,我想我可能有一些对你有用的东西.为此,您需要在数据库中有一个列(我将其称为 attachment_storage),您可以在序列化这些附件后将其存储在其中.

基本上你想先把附件放入数组中,然后将其序列化为字符串,以便将其存储到数据库中.

这里有一些代码可以完成这项工作.

attachment_storage = [](1..params[:attachments].to_i).每个都做 |attachment_num|附件存储<<参数[附件#{attachment_num}".to_sym]结尾

在这里,我们使用字符串和 to_sym 为 params 哈希构建符号,将其转换为类似 :attachment1:attachment2 等的符号.

然后您想放入数据库,以便您可以按照[ActiveRecord 文档][1] 中在文本列中保存数组、散列和其他不可映射对象"部分下的说明进行存储.

为了使序列化工作,您需要将 serialize :attachment_storage 添加到您的模型中,然后将其存储,就像上面的任何其他参数一样分配它.然后保存您的模型,它将为您序列化.

I want to serialize any object(s) in text columns.

Via an API, I get the params:

params[:attachments] -- this can be 0 or 1 or 3, or 100+ etc...
params[:attachment1]...params[:attachment2] ... params[:attachmentN]

So how do I store X # of attachments in a serialized object?

mailThing = MailThing.create(:attachments => myAttachmentsSerizliedIfANY )

I'm trying to do:

@myAttachmentsSerizliedIfANY = nil

i = 0
attachmentCount = params[:attachments].to_i
while i < attachmentCount do

   @myAttachmentsSerizliedIfANY << params[:attachment + i ]

   i += 1
end

Any suggestions on how to get this working? thanks

解决方案

Okay, so I looked at some of your other questions, and I think I might have something that will work for you. For this to work, you will need to have a column in the database (I'll call it attachment_storage) where you can store these attachments after you serialize them.

Basically you want to get the attachments into an array first, and then serialize it into a string so that you can store it into the database.

Here's some code to get that done.

attachment_storage = []
(1..params[:attachments].to_i).each do |attachment_num|
   attachment_storage << params["attachment#{attachment_num}".to_sym]
end

Here we're building the symbols for the params hash using the string and to_sym to turn it into a symbol like :attachment1, :attachment2, etc.

Then you want to put in the database, so you can store it as noted in the [ActiveRecord Documentation][1] under the section "Saving arrays, hashes, and other non-mappable objects in text columns".

In order for the serialization to work, you need to add serialize :attachment_storage to your model, and then to store it you would assign it just like any other parameter as above. Then save your model and it will be serialized for you.

这篇关于Rails 构建 N 条记录的序列化对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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