Module: Mongoid::Serialization

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Serialization
Included in:
Components
Defined in:
lib/mongoid/serialization.rb

Overview

This module provides the extra behaviour for including relations in JSON and XML serialization.

Instance Method Summary collapse

Instance Method Details

#serializable_hash(options = nil) ⇒ Hash

Gets the document as a serializable hash, used by ActiveModel’s JSON and XML serializers. This override is just to be able to pass the :include and :except options to get associations in the hash.

Examples:

Get the serializable hash.

document.serializable_hash

Get the serializable hash with options.

document.serializable_hash(:include => :addresses)

Parameters:

  • options (Hash) (defaults to: nil)

    The options to pass.

Options Hash (options):

  • :include (Symbol)

    What relations to include

  • :only (Symbol)

    Limit the fields to only these.

  • :except (Symbol)

    Dont include these fields.

Returns:

  • (Hash)

    The document, ready to be serialized.

Since:

  • 2.0.0.rc.6



29
30
31
32
33
34
# File 'lib/mongoid/serialization.rb', line 29

def serializable_hash(options = nil)
  options ||= {}
  super(options).tap do |attrs|
    serialize_relations(attrs, options) if options[:include]
  end
end