Module: ROM::SQL::Plugin::Associates::ClassMethods Private

Defined in:
lib/rom/sql/plugin/associates.rb

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

Instance Method Summary collapse

Instance Method Details

#associates(name, options) ⇒ Object

Set command to associate tuples with a parent tuple using provided keys

Examples:

class CreateTask < ROM::Commands::Create[:sql]
  relation :tasks
  associates :user, key: [:user_id, :id]
end

create_user = rom.command(:user).create.with(name: 'Jane')

create_tasks = rom.command(:tasks).create
  .with [{ title: 'One' }, { title: 'Two' } ]

command = create_user >> create_tasks
command.call

Parameters:

  • name (Symbol)

    The name of associated table

  • options (Hash)

    The options

Options Hash (options):

  • :key (Array)

    The association keys



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rom/sql/plugin/associates.rb', line 65

def associates(name, options)
  if associations.include?(name)
    raise(
      ArgumentError,
      "#{name} association is already defined for #{self.class}"
    )
  end

  option :association, reader: true, default: -> command { options }
  include InstanceMethods

  associations << name
end

#inherited(klass) ⇒ Object

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.



38
39
40
41
42
# File 'lib/rom/sql/plugin/associates.rb', line 38

def inherited(klass)
  klass.defines :associations
  klass.associations []
  super
end