Skip to content

Defining and importing Complex Associations

tom statter edited this page Jan 14, 2015 · 14 revisions

Syntax Summary

Associations

Datashift can load model data, and manage and create relationships with Associated models (tables) from a single file row. The column heading can contain the association name, plus options for finding that association

It supports a variety of mechanisms for specifying the key field for the lookups required, and to specify multiple associations in a single column, or multiple columns.

The syntax uses a default set of delimiters, defined in Delimiters (delimiters.rb) to define association name, association lookup key, and associations value(s). The delimiters used can however, be set through configuration options.

Example : Loading Users. Column Heading : Project:name:My Best Project

User (klass) has_one project (operator) lookup by name (find_by_operator) == 'My Best Project' (find_by_value)

The loader essentially calls : User.project.where( :name => 'My Best Project')

Associations

Create associations to a base object in a single column.

Each entry represents the association name, the field on the association to use in the search, and optional default field value to find the association instance.

AssocName:value

Otherwise, each row can specify the field value to use to find the association instance.

Example: Each row represents a User. Example from single 'Department' column, where User belongs_to Department which has a name.

  User|.....|Department:name
  abc|......|English
  xyz|......|Maths

Gives

    user.department = Department.where( :name => 'Maths') 
Size:small            ```generates => .where( :size => 'small' )
Colour:red            ```generates =< .where( :colour => 'red' )

Multiple Associations

For has_many associations. multiple items can be added to a base object, either via multiple columns or even in a single column.

Each entry represents the association name, and the value to use to find the association instance. For single column usage, multiple entries (name/value pairs) should be separated by the multi_assoc_delim default '|'

AssocName:value|AssocName:value2|AssocName:value3

e.g Each row representsa User. Example from single 'Roles' column, where User has_many Roles and each Role has a name.

Name:tester|Name:manager|Name:auditor  

Gives

    user.roles = [ Role.find_by_name( 'tester'), Role.find_by_name('manager'), Role.find_by_name('auditor') ]
Colour:red,green,blue # => generates find_all_by_colour( ['red','green','blue'] )

 jumper.colours = [red, green, blue]

A composite Variant can be created by supplying values for multiple OptionType such as Colour : Red and Size Medium (n.b for CSV you'll need to escape the ',')

colour: red, green, blue, orange => Creates 4 Variants with single colour OptionType each

Combine Multiple Associations

Some loaders or use cases support combing associations.

A good example is the Variant's of a Spree Product, which can be created with multiple OptionTypes

You can combine optiontypes into single Variants using ;

colour:blue;size:small | colour:red;size:medium | colour:green;size:small => Creates 3 Variants with 2 OptionTypes each

And very terse, share a single option value with multiple values of another OptionType

colour: red, green, blue, orange; size: small  => Creates 4 Variants, all with size small but individual colours