-
Notifications
You must be signed in to change notification settings - Fork 27
Serialization example
Sharagoz edited this page May 15, 2012
·
4 revisions
ActiveRecord serialization has been available since Rails 3.0. (The more advanced ActiveRecord storage has been available since Rails 3.2). This enables you to store and retrieve raw ruby objects in the database.
Using this feature is as simple as telling active_record which field(s) should be serialized:
class MyModel < ActiveRecord::Base
serialize :my_field
endExample: Storing the environment info in a single database field using serialization, so that we dont have to create separate fields for each one.
config.store_environment_info do |storage,env|
storage[:env_info] = {
:path_info => env["PATH_INFO"],
:query_string => env["QUERY_STRING"],
:remote_addr => env["REMOTE_ADDR"],
:remote_host => env["REMOTE_HOST"],
:request_method => env["REQUEST_METHOD"],
:request_uri => env["REQUEST_URI"],
:script_name => env["SCRIPT_NAME"],
:server_name => env["SERVER_NAME"],
:server_port => env["SERVER_PORT"]
}
end
# Monkey patching the model that comes with rails_exception_handler to use serialization:
config.after_initialize do
ErrorMessage.send(:serialize, :env_info)
end