`
jbf034
  • 浏览: 152400 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Storing Structured Data(序列化数据)

阅读更多
有时候这样做很方便,存储包含任意Ruby 对象的属性直接到数据库的表中。Active
Record 通过序列化Ruby 对象到一个字符串中(使用YAML 格式)来支持这种方式,并且存储这
个字符串到数据库内对应此属性的列中。在schema 中,这个列必须被定义为text 类型。
因为Active Record 通常将映射一个character 或text 列为一个纯Ruby 字符串,在你
想获得这个功能的优势时,你需要告诉Active Record 使用序列化。例如,我们想记录最后
五位购买者做为我们客户。我们将创建一个包含一个text 列的表来保存这些信息。
create table purchases (
id int not null auto_increment,
name varchar(100) not null,
last_five text,
primary key (id)
);
在Active Record 类中包装这个表,我们将使用serialize()声明来告诉Active Record
要marshal 对象到这个列中。
class Purchase < ActiveRecord::Base
serialize :last_five
# ...
end
当我们创建新的Purchase 对象时,我们可以给last_five 列赋值任何Ruby 对象。在这
个例子中,我们设置它为一个字符串数组。
purchase = Purchase.new
purchase.name = "Dave Thomas"
purchase.last_five = [ 'shoes', 'shirt', 'socks', 'ski mask',
'shorts' ]
purchase.save
当稍后我们读它时,属性被设置回一个数组。
purchase = Purchase.find_by_name("Dave Thomas")
pp purchase.last_five
pp purchase.last_five[3]
这个代码输出
["shoes", "shirt", "socks", "ski mask", "shorts"]
"ski mask"
尽管强大和方便,如果你想在Ruby 应用程序的外部来使用序列化内的信息还有些问题。
除非应用程序理解YAML 格式,因为列内容对它是不透明的。事实上,很难在SQL 查询的内部
使用结构(structure)。相反,你可能考虑使用对象聚合来代替,它描述在247 页15.2 节,
来达到同样的效果。
分享到:
评论

相关推荐

    Yoshida_Hiroshi_Experiments_in_Storing_Data__Cold_Storage_Services.pdf

    在《Yoshida_Hiroshi_Experiments_in_Storing_Data__Cold_Storage_Services.pdf》这份文档中,作者Hiroshi Yoshida对科学数据在云冷存储服务中的存储进行了详细的探讨与实验分析。该文档主要介绍了云冷存储的概念、...

    Oracle Hyperion Essbase - Storing Analytic Data - 培训资料

    在块存储中,数据被组织成数据块(Data Blocks)和索引(Index): - **数据块**:由密集维度定义,每个数据块包含一组密集维度成员组合下的数据值。 - **索引**:由稀疏维度定义,用于记录数据块中每个元素的位置...

    Swift library for making storing data in a SQLite database easy as pie.zip

    Swift library for making storing data in a SQLite database easy as pie.zip,Swift library for making storing data in a SQLite database simple and magic-free

    Data.Lake.Architecture.1634621174

    Organizations invest incredible amounts of time and money obtaining and then storing big data in data stores called data lakes. But how many of these organizations can actually get the data back out ...

    《iOS Core Data高级编程 第二版》两本IOS数据编程之一

    第三章“Storing Data: SQLite and Other Options”探讨了如何使用SQLite存储数据,同时也讨论了其他存储选项,例如二进制数据、内存存储等。 - **创建数据模型**:数据模型是Core Data应用程序的架构蓝图。第四章...

    Service-generated Big Data and Big Data-as-a-Service: An Overview

    used for storing and managing Big Data. Analyzing Big Data is a challenging task as it involves large distributed file systems which should be fault tolerant, flexible and scalable. Map Reduce is ...

    Pro Core Data for IOS

    第三章“Storing Data: SQLite and Other Options”探讨了数据存储的多种方式,特别是SQLite的使用,也涵盖了其他可选的存储方案。 在第四章“Creating a Data Model”中,介绍了如何创建和管理数据模型,这是使用...

    漏洞数据集,漏洞数据集NVD.zip

    Set up an empty MySQL database for storing NVD data, and put the database connection information into config.txt in a directory where you want to run the MulVAL adapters. Example config.txt: ...

    Handbook of Big Data Technologies

    2 Storing Linked Data Using Relational Databases 3 No-SQL Stores 4 Massively Parallel Processing for Linked Data Exploratory Ad-Hoc Analytics for Big Data 1 Exploratory Analytics for Big Data 2 A Top...

    Head First Python , Second Edition by Paul Barry 2016年出版 英文版

    3 Structured Data: Working with Structured Data 95 4 Code Reuse: Functions and Modules 145 5 Building a Webapp: Getting Real 195 6 Storing and Manipulating Data: Where to Put Your Data 243 7 Using a ...

    Python Data Analytics 无水印pdf 0分

    This book is an invaluable reference with its examples of storing and accessing data in a database; it walks you through the process of report generation; it provides three real world case studies or ...

    Network Storage Tools and Technologies for Storing Your Company's Data 无水印pdf

    Network Storage Tools and Technologies for Storing Your Company's Data 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 ...

    Pentaho_Data_Integration_4_Cookbook

    data transformation is where the data is transformed for storing in the proper format or structure for the purposes of querying and analysis; data loading where the data is loaded into the final ...

    web scraping with python collecting more data from the modern web 2nd

    Learn several methods for storing the data you scrape Download, read, and extract data from documents Use tools and techniques to clean badly formatted data Read and write natural languages Crawl ...

    Pro Core Data for iOS.pdf

    **重要概念**:Core Data是iOS平台上的一个对象持久化框架,它允许开发者以面向对象的方式存储和检索应用程序数据。 **发展历程**:自从iOS引入数据持久化的概念以来,Core Data逐步成为存储和管理数据的主要方式。...

    Network Storage Tools and Technologies for Storing Your Company's Data

    随着信息技术的迅速发展,企业数据量呈指数级增长,如何有效地管理和存储这些数据成为了一个重要的课题。网络存储技术作为解决这一问题的关键手段之一,其重要性不言而喻。本文将深入探讨《网络存储工具和技术用于...

Global site tag (gtag.js) - Google Analytics