将多个对象保存到表中的单个字段

时间:2022-09-15 21:17:13

I want to save a set of email addresses collected from a text field (separated by commas) into a single field in a table. I am trying to use serialize but I'm getting SerializationTypeMismatch error. How can I do this?

我想将从文本字段(用逗号分隔)收集的一组电子邮件地址保存到表中的单个字段中。我试图使用序列化,但我收到SerializationTypeMismatch错误。我怎样才能做到这一点?

This is my model:

这是我的模特:

class Crew < ActiveRecord::Base

class Crew

class Recipients < HashWithIndifferentAccess ; end

class Recipients ;结束

serialize :recipients, Recipients

序列化:收件人,收件人

end

2 个解决方案

#1


3  

I would really reccomend that you parse (e.g. split on comma) the email list and put each in a row in a separate table (I assume you're talkin about a database table?). If you want to use the email addresses for something it's better to store them individually, and since you're talking about serialization I guess you've allready parsed the emails, and try to store an array or similar into a single field in a database. The "correct" normalized database way of doing this is to in your model wich you're currently trying to save the object, add a has_many :emails (or similar) and creat a new email table for each email.

我真的建议您解析(例如,在逗号上拆分)电子邮件列表,并将每个列表放在一个单独的表中(我假设您正在谈论数据库表?)。如果你想使用电子邮件地址,最好单独存储它们,并且因为你在讨论序列化,我想你已经完成了解析电子邮件,并试图将数组或类似的东西存储到数据库的单个字段中。 “正确”的规范化数据库方式是在您的模型中,您当前正在尝试保存对象,添加has_many:电子邮件(或类似)并为每封电子邮件创建一个新的电子邮件表。

One should allways have a very good reason for storing list type data in a blob, instead of using a proper associated table.

人们总是有充分的理由将列表类型数据存储在blob中,而不是使用适当的关联表。

#2


1  

If you are given a comma separated list of email addresses, and if when you need to use that information, a comma separated list is useful, then just put it in a text column in your model, without serialization.

如果给您一个逗号分隔的电子邮件地址列表,并且当您需要使用该信息时,以逗号分隔的列表很有用,那么只需将其放在模型的文本列中,而不进行序列化。

If you need to serialize the list, wouldn't it be an array? Can you explain the choice of a hash?

如果你需要序列化列表,它不是一个数组吗?你能解释一下哈希的选择吗?

#1


3  

I would really reccomend that you parse (e.g. split on comma) the email list and put each in a row in a separate table (I assume you're talkin about a database table?). If you want to use the email addresses for something it's better to store them individually, and since you're talking about serialization I guess you've allready parsed the emails, and try to store an array or similar into a single field in a database. The "correct" normalized database way of doing this is to in your model wich you're currently trying to save the object, add a has_many :emails (or similar) and creat a new email table for each email.

我真的建议您解析(例如,在逗号上拆分)电子邮件列表,并将每个列表放在一个单独的表中(我假设您正在谈论数据库表?)。如果你想使用电子邮件地址,最好单独存储它们,并且因为你在讨论序列化,我想你已经完成了解析电子邮件,并试图将数组或类似的东西存储到数据库的单个字段中。 “正确”的规范化数据库方式是在您的模型中,您当前正在尝试保存对象,添加has_many:电子邮件(或类似)并为每封电子邮件创建一个新的电子邮件表。

One should allways have a very good reason for storing list type data in a blob, instead of using a proper associated table.

人们总是有充分的理由将列表类型数据存储在blob中,而不是使用适当的关联表。

#2


1  

If you are given a comma separated list of email addresses, and if when you need to use that information, a comma separated list is useful, then just put it in a text column in your model, without serialization.

如果给您一个逗号分隔的电子邮件地址列表,并且当您需要使用该信息时,以逗号分隔的列表很有用,那么只需将其放在模型的文本列中,而不进行序列化。

If you need to serialize the list, wouldn't it be an array? Can you explain the choice of a hash?

如果你需要序列化列表,它不是一个数组吗?你能解释一下哈希的选择吗?