Class: ROM::Changeset::Associated

Inherits:
Object
  • Object
show all
Extended by:
Initializer
Defined in:
lib/rom/repository/changeset/associated.rb

Overview

Associated changesets automatically set up FKs

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#associationSymbol (readonly)

Returns Association identifier from relation schema.

Returns:

  • (Symbol)

    Association identifier from relation schema



21
# File 'lib/rom/repository/changeset/associated.rb', line 21

option :association, reader: true

#leftChangeset::Create (readonly)

Returns Child changeset.

Returns:



13
# File 'lib/rom/repository/changeset/associated.rb', line 13

param :left

#rightChangeset::Create, ... (readonly)

Returns Parent changeset or data.

Returns:



17
# File 'lib/rom/repository/changeset/associated.rb', line 17

param :right

Instance Method Details

#commandROM::Command::Composite

Create a composed command

This works only with parent => child(ren) changeset hierarchy

Examples:

using existing parent data

user_changeset = user_repo.changeset(name: 'Jane')
task_changeset = task_repo.changeset(title: 'Task One')

user = user_repo.create(user_changeset)
task = task_repo.create(task_changeset.associate(user, :user))

saving both parent and child in one go

user_changeset = user_repo.changeset(name: 'Jane')
task_changeset = task_repo.changeset(title: 'Task One')

task = task_repo.create(task_changeset.associate(user, :user))

Returns:

  • (ROM::Command::Composite)


59
60
61
62
63
64
65
66
67
68
# File 'lib/rom/repository/changeset/associated.rb', line 59

def command
  case right
  when Changeset
    left.command.curry(left) >> right.command.with_association(association).curry(right)
  when Associated
    left.command.curry(left) >> right.command.with_association(association)
  else
    left.command.with_association(association).curry(left, right)
  end
end

#commitArray<Hash>, Hash

Commit changeset’s composite command

Examples:

task_changeset = task_repo.
  changeset(title: 'Task One').
  associate(user, :user).
  commit
# {:id => 1, :user_id => 1, title: 'Task One'}

Returns:

  • (Array<Hash>, Hash)


35
36
37
# File 'lib/rom/repository/changeset/associated.rb', line 35

def commit
  command.call
end

#relationObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



71
72
73
# File 'lib/rom/repository/changeset/associated.rb', line 71

def relation
  left.relation
end