Class: ROM::SQL::Commands::Update

Inherits:
Commands::Update
  • Object
show all
Includes:
Deprecations, ErrorWrapper, Transaction
Defined in:
lib/rom/sql/commands/update.rb

Overview

Update command

Instance Method Summary collapse

Methods included from ErrorWrapper

#call

Methods included from Transaction

#transaction

Instance Method Details

#change(original) ⇒ Command::Update

Update existing tuple only when it changed

Examples:

user = rom.relation(:users).one
new_user = { name: 'Jane Doe' }

rom.command(:users).change(user).call(new_user)

Parameters:

  • original (#to_h, Hash)

    The original tuple

Returns:

  • (Command::Update)


57
58
59
# File 'lib/rom/sql/commands/update.rb', line 57

def change(original)
  self.class.build(relation, options.merge(original: original.to_h))
end

#execute(tuple) ⇒ Array<Hash>, Hash

Updates existing tuple in a relation

Returns:

  • (Array<Hash>, Hash)


31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rom/sql/commands/update.rb', line 31

def execute(tuple)
  attributes = input[tuple]
  validator.call(attributes)

  changed = diff(attributes.to_h)

  if changed.any?
    update(changed)
  else
    []
  end
end