Skip to content
This repository has been archived by the owner on Sep 7, 2024. It is now read-only.

Denormalization definition & options

alvinsj edited this page Aug 19, 2014 · 6 revisions

1. Denormalization definition

denormalize :target_model, denormalization_options_hash
  • :target_model: symbol of the target model to specify which existing model to denormalize.
  • denormalization_options_hash: hash of options (see below at 2. Denormalization options)

2. Denormalization options

Options for columns

:only

{ only: array_of_columns, ..

array_of_column: array of column names to save from the target model

:except

{ except: array_of_columns, ..

array_of_columns: array of column names to not save from the target model

Note: Specify only ONE between :only or :except

:methods

{ methods: hash_with_columns, ..

hash_with_columns: hash of columns in the format of column_name: sql_type_or_options to specify which existing method(s) in your target model will be saved.

sql_type_or_options: symbol of sql_type OR hash of column options (e.g. type, null, default)

:compute

{ compute: hash_with_columns, ..

hash_with_columns: hash of columns in the format of column_name: :sql_type_or_options to specify which existing method(s) in your denormalized model will be saved.

sql_type_or_options: symbol of sql_type OR hash of column options (e.g. type, null, default)

Note: You need to prefix the column_name with compute_

For example:

denormalize :order, { 
	compute: { 
		line_item_cost: {type: decimal, default: 0, null: false} 
	},
...

def compute_line_item_cost # compute_[column_name]
	# implementation details
end

Options for association

:include & :class_name

{ include: hash_with_association, ..

hash_with_association: hash of association in the format of model_name: denormalization_options_hash to specify which association to denormalize, and the denormalization options.

Note:
1. Association must be specified in the target model (e.g. has_many: line_items) before use.
2. Use class_name: class_name_in_string if it's a polymorphic association.

For example:

denormalize :order, { 
	include: { 
		line_item: { 
			include: { 
				saleable: { 
					class_name: :phone, 
					..
				},
			...
			}
		} 
	},
...

Other options

:prefix

{ prefix: 'prefix_in_string, ..

prefix_in_string: string of the prefix to enable custom prefix, rather than using generated column name.

For example:

denormalize :order, { 
	include: { 
		line_item: { 
			include: { 
				saleable: { 
					class_name: :phone, 
					prefix: 'sold_phone_',
					only: [:brand]
					..
				},
			...
			}
		} 
	},
...

The default generated column name for brand is order_line_item_saleable_phone_brand. By adding prefix: 'sold_phone_', the generated column name will become sold_phone_branch instead.