Rails Marshal 简单例子讲解
2015-06-26 07:37
357 查看
1. Why use Marshal?
Basically anytime you want to store a whole object in byte stream and need to retrieve the object from the byte stream later.
This is the situation I encountered and also why I decided to write this blog:
1.1 I had an object that logically should be stored in session, but it was a relatively big object, so I need to store it in the database.
1.2 Since the attributes of this object could change, which meant I couldn’t create each database column based on the attribute names and types.
For example, see the user_obj below, it now has ‘name, age, address, weight, height’ attributes, but it might have more attributes or the type of the attributes could change.
1.3 So I decided to store the object as a serialized object in one column of the database.
2. The following is a simple example:
2.1 Use the following Database migration file to create a User table with user_id, user_info, created_at and updated_at fields.
db/migrate/20150625130210_create_user.rb:
Reminder: user_info needs to be type ‘binary‘.
I tried ‘text’, it worked but it caused my system to beep.
2.2 We have a class like this:
So we can create an obejct like this:
2.3 Now we want to store this object as a serialized object in the user_info field in User table.
2.3.1 Create the serialized object:
2.3.2 Save the object to database:
2.4 Later, if we want to retrieve this object. we can do like this:
2.4.1 Retrieve the serialized object from database:
2.4.2 Deserialize this object:
3. Marshal official reference:
http://ruby-doc.org/core-2.1.5/Marshal.html
Basically anytime you want to store a whole object in byte stream and need to retrieve the object from the byte stream later.
This is the situation I encountered and also why I decided to write this blog:
1.1 I had an object that logically should be stored in session, but it was a relatively big object, so I need to store it in the database.
1.2 Since the attributes of this object could change, which meant I couldn’t create each database column based on the attribute names and types.
For example, see the user_obj below, it now has ‘name, age, address, weight, height’ attributes, but it might have more attributes or the type of the attributes could change.
1.3 So I decided to store the object as a serialized object in one column of the database.
2. The following is a simple example:
2.1 Use the following Database migration file to create a User table with user_id, user_info, created_at and updated_at fields.
db/migrate/20150625130210_create_user.rb:
class CreateUser < ActiveRecord::Migration def change create_table :user do |t| t.integer :user_id t.binary:user_info t.timestamps null: false end end end
Reminder: user_info needs to be type ‘binary‘.
I tried ‘text’, it worked but it caused my system to beep.
2.2 We have a class like this:
class UserInfo attr_reader :name, :age, :address, :weight, :height def initialize(name, age, address, weight, height) @name = name @age = age @address = address @weigtht = weight @height = height end end
So we can create an obejct like this:
user_obj = UserInfo.new(‘christy’, 22, ‘1021 penn circle’, ’50’, ‘160’)
2.3 Now we want to store this object as a serialized object in the user_info field in User table.
2.3.1 Create the serialized object:
user_serialized_obj = Marshal.dump(user_obj)
2.3.2 Save the object to database:
User.create(user_id: 1, user_info: user_serialized_obj)
2.4 Later, if we want to retrieve this object. we can do like this:
2.4.1 Retrieve the serialized object from database:
user_serialized_obj = User.find(1).user_info
2.4.2 Deserialize this object:
user_obj = Marshal.load(user_serialized_obj)
3. Marshal official reference:
http://ruby-doc.org/core-2.1.5/Marshal.html
相关文章推荐
- mysql load data 导出、导入 csv
- 红宝石(Ruby)史话
- Ruby简明教程之循环语句介绍
- ruby 一些简单的例子
- Ruby 之 class 中的 private、 protected、public
- Ruby中操作文件的方法介绍
- CentOS 6.3下编译安装Ruby 2.0笔记
- Ruby编程中关于中断和返回的用法教程
- Ruby中的变量学习总结
- 几个加速Ruby on Rails的编程技巧
- 分析Cache 在 Ruby China 里面的应用情况
- Ruby中数组的一些相关使用方法
- Ruby类继承、抽象类、类拓展混入、代理类实例
- Ruby简明教程之方法(Method)介绍
- 比较详细的ruby symbol 学习资料
- Ruby定义私有方法(private)的两种办法
- Ruby基础知识之方法、代码段
- ruby 数组使用教程
- Ruby中的String对象学习笔记
- MySQL分库分表备份脚本