Ruby-on-rails Rails:序列化数据库中的对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2959661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:44:07  来源:igfitidea点击:

Rails: Serializing objects in a database?

ruby-on-railsrubyserializationmodelobject

提问by keruilin

I'm looking for some general guidance on serializing objects in a database.

我正在寻找有关序列化数据库中对象的一般指南。

  1. What are serialized objects?
  2. What are some best-practice scenarios for serializing objects in a DB?
  3. What attributes do you use when creating the column in the DB so you can use a serialized object?
  4. How to save a serialized object?
  5. And how to access the serialized object and its attributes? (Using hashes?)
  1. 什么是序列化对象?
  2. 在数据库中序列化对象的最佳实践场景有哪些?
  3. 在数据库中创建列时使用什么属性,以便可以使用序列化对象?
  4. 如何保存序列化对象?
  5. 以及如何访问序列化对象及其属性?(使用哈希?)

回答by severin

In computer science, in the context of data storage and transmission, serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file, a memory buffer, or transmitted across a network connection link to be "resurrected" later in the same or another computer environment. (see http://en.wikipedia.org/wiki/Serialization)

在计算机科学中,在数据存储和传输的上下文中,序列化是将数据结构或对象转换为位序列以便将其存储在文件、内存缓冲区或通过网络连接链接传输的过程稍后在相同或其他计算机环境中“复活”。(见http://en.wikipedia.org/wiki/Serialization

  1. So serialized objects (in the context of ActiveRecord) are text/string representations of objects (encoded using YAML). When serialized, you can save (almost) any Ruby object in a single database field.

  2. You can use serialization if you have somewhat complex objects that you need to save in a database and you don't need to retrieve records based on the contents of a serialized attribute. I used them for example for storing preferences for users of a webapp: the preferences were basically hashes that I wanted to save in a single db field.

  1. 因此序列化对象(在 ActiveRecord 的上下文中)是对象的文本/字符串表示(使用 YAML 编码)。序列化后,您可以将(几乎)任何 Ruby 对象保存在单个数据库字段中。

  2. 如果您需要将一些复杂的对象保存在数据库中并且不需要根据序列化属性的内容检索记录,则可以使用序列化。例如,我使用它们来存储 webapp 用户的首选项:首选项基本上是我想保存在单个 db 字段中的哈希值。

3./4./5. Use ActiveRecord::Base.serializeas Marc-André Lafortune suggested:

3./4./5. 按照 Marc-André Lafortune 的建议使用ActiveRecord::Base.serialize

class User < ActiveRecord::Base
  serialize :preferences
end

u = User.new
u.preferences = {:show_tooltips => true, :use_extended_menu => false, ...}
u.save

# ...

u = User.find(23)
u.preferences # => {:show_tooltips => true, :use_extended_menu => false, ...}

回答by Marc-André Lafortune

Really easy. Check the docfor ActiveRecord::Base.serialize.

真的很容易。检查文档以获取ActiveRecord::Base.serialize.

回答by astropanic

Have a look at MessagePack

看看消息包

回答by Gautham

If you are using Rails 4 with Postgresql, Rails 4 supports hstore field which can store hashes and allows queries too. http://blog.remarkablelabs.com/2012/12/a-love-affair-with-postgresql-rails-4-countdown-to-2013

如果您将 Rails 4 与 Postgresql 一起使用,Rails 4 支持 hstore 字段,它可以存储哈希值并允许查询。 http://blog.remarkablelabs.com/2012/12/a-love-affair-with-postgresql-rails-4-countdown-to-2013