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) (defaults to: {})

    The options

Options Hash (options):

  • :key (Array)

    The association keys



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rom/sql/plugin/associates.rb', line 105

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

  option :association, reader: true, default: {}

  include InstanceMethods

  associations << [name, options]
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.



78
79
80
81
82
# File 'lib/rom/sql/plugin/associates.rb', line 78

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