Class: Sequel::Model::Associations::ManyToManyAssociationReflection

Inherits:
AssociationReflection show all
Defined in:
lib/sequel/model/associations.rb

Constant Summary collapse

FINALIZE_SETTINGS =
superclass::FINALIZE_SETTINGS.merge(
  :associated_key_array=>:associated_key_array,
  :qualified_right_key=>:qualified_right_key,
  :join_table_source=>:join_table_source,
  :join_table_alias=>:join_table_alias,
  :qualified_right_primary_key=>:qualified_right_primary_key,
  :right_primary_key=>:right_primary_key,
  :right_primary_keys=>:right_primary_keys,
  :right_primary_key_method=>:right_primary_key_method,
  :right_primary_key_methods=>:right_primary_key_methods,
  :select=>:select
).freeze

Constants inherited from AssociationReflection

AssociationReflection::ASSOCIATION_DATASET_PROC

Instance Method Summary collapse

Methods inherited from AssociationReflection

#_add_method, #_remove_all_method, #_remove_method, #_setter_method, #add_method, #apply_dataset_changes, #apply_distinct_on_eager_limit_strategy, #apply_eager_dataset_changes, #apply_eager_graph_limit_strategy, #apply_eager_limit_strategy, #apply_ruby_eager_limit_strategy, #apply_window_function_eager_limit_strategy, #assign_singular?, #associated_class, #associated_dataset, #association_dataset_for, #association_dataset_proc, #association_method, #dataset_method, #dataset_need_primary_key?, #delete_row_number_column, #eager_graph_lazy_dataset?, #eager_graph_limit_strategy, #eager_limit_strategy, #eager_load_results, #eager_loader_key, #filter_by_associations_add_conditions?, #filter_by_associations_conditions_expression, #finalize, #handle_silent_modification_failure?, #initialize_association_cache, #inspect, #limit_and_offset, #placeholder_loader, #predicate_key_values, #predicate_keys, #qualify, #qualify_assoc, #qualify_cur, #reciprocal, #reciprocal_array?, #remove_all_method, #remove_before_destroy?, #remove_method, #remove_should_check_existing?, #returns_array?, #set_reciprocal_to_self?, #setter_method, #slice_range

Methods included from Inflections

clear, irregular, plural, singular, uncountable

Methods inherited from Hash

#&, #case, #hstore, #pg_json, #pg_jsonb, #sql_expr, #sql_negate, #sql_or, #|, #~

Instance Method Details

#associated_key_aliasObject

The alias to use for the associated key when eagerly loading



1224
1225
1226
# File 'lib/sequel/model/associations.rb', line 1224

def associated_key_alias
  self[:left_key_alias]
end

#associated_key_arrayObject

Array of associated keys used when eagerly loading.



1229
1230
1231
1232
1233
1234
1235
1236
1237
# File 'lib/sequel/model/associations.rb', line 1229

def associated_key_array
  cached_fetch(:associated_key_array) do
    if self[:uses_left_composite_keys]
      associated_key_alias.zip(predicate_keys).map{|a, k| SQL::AliasedExpression.new(k, a)}
    else
      [SQL::AliasedExpression.new(predicate_key, associated_key_alias)]
    end
  end
end

#associated_key_columnObject

The column to use for the associated key when eagerly loading



1240
1241
1242
# File 'lib/sequel/model/associations.rb', line 1240

def associated_key_column
  self[:left_key]
end

#associated_object_keysObject

Alias of right_primary_keys



1245
1246
1247
# File 'lib/sequel/model/associations.rb', line 1245

def associated_object_keys
  right_primary_keys
end

#can_have_associated_objects?(obj) ⇒ Boolean

many_to_many associations can only have associated objects if none of the :left_primary_keys options have a nil value.

Returns:

  • (Boolean)


1251
1252
1253
# File 'lib/sequel/model/associations.rb', line 1251

def can_have_associated_objects?(obj)
  !self[:left_primary_keys].any?{|k| obj.get_column_value(k).nil?}
end

#cloneable?(ref) ⇒ Boolean

one_through_one and many_to_many associations can be clones

Returns:

  • (Boolean)


1256
1257
1258
# File 'lib/sequel/model/associations.rb', line 1256

def cloneable?(ref)
  ref[:type] == :many_to_many || ref[:type] == :one_through_one
end

#default_associated_key_aliasObject

The default associated key alias(es) to use when eager loading associations via eager.



1262
1263
1264
# File 'lib/sequel/model/associations.rb', line 1262

def default_associated_key_alias
  self[:uses_left_composite_keys] ? (0...self[:left_keys].length).map{|i| :"x_foreign_key_#{i}_x"} : :x_foreign_key_x
end

#default_eager_loader(eo) ⇒ Object

The default eager loader used if the user doesn’t override it. Extracted to a method so the code can be shared with the many_through_many plugin.



1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
# File 'lib/sequel/model/associations.rb', line 1268

def default_eager_loader(eo)
  h = eo[:id_map]
  assign_singular = assign_singular?
  delete_rn = delete_row_number_column
  uses_lcks = self[:uses_left_composite_keys]
  left_key_alias = self[:left_key_alias]
  name = self[:name]

  self[:model].eager_load_results(self, eo) do |assoc_record|
    assoc_record.values.delete(delete_rn) if delete_rn
    hash_key = if uses_lcks
      left_key_alias.map{|k| assoc_record.values.delete(k)}
    else
      assoc_record.values.delete(left_key_alias)
    end

    objects = h[hash_key]

    if assign_singular
      objects.each do |object| 
        object.associations[name] ||= assoc_record
      end
    else
      objects.each do |object|
        object.associations[name].push(assoc_record)
      end
    end
  end
end

#default_join_tableObject

Default name symbol for the join table.



1299
1300
1301
# File 'lib/sequel/model/associations.rb', line 1299

def default_join_table
  [self[:class_name], self[:model].name].map{|i| underscore(pluralize(demodulize(i)))}.sort.join('_').to_sym
end

#default_left_keyObject

Default foreign key name symbol for key in join table that points to current table’s primary key (or :left_primary_key column).



1305
1306
1307
# File 'lib/sequel/model/associations.rb', line 1305

def default_left_key
  :"#{underscore(demodulize(self[:model].name))}_id"
end

#default_right_keyObject

Default foreign key name symbol for foreign key in join table that points to the association’s table’s primary key (or :right_primary_key column).



1311
1312
1313
# File 'lib/sequel/model/associations.rb', line 1311

def default_right_key
  :"#{singularize(self[:name])}_id"
end

#eager_loading_use_associated_key?Boolean

many_to_many associations need to select a key in an associated table to eagerly load

Returns:

  • (Boolean)


1344
1345
1346
# File 'lib/sequel/model/associations.rb', line 1344

def eager_loading_use_associated_key?
  !separate_query_per_table?
end

#finalize_settingsObject



1327
1328
1329
# File 'lib/sequel/model/associations.rb', line 1327

def finalize_settings
  FINALIZE_SETTINGS
end

#join_table_aliasObject Also known as: associated_key_table

The join table itself, unless it is aliased, in which case this is the alias.



1356
1357
1358
1359
1360
1361
# File 'lib/sequel/model/associations.rb', line 1356

def join_table_alias
  cached_fetch(:join_table_alias) do
    s, a = split_join_table_alias
    a || s
  end
end

#join_table_sourceObject

The source of the join table. This is the join table itself, unless it is aliased, in which case it is the unaliased part.



1350
1351
1352
# File 'lib/sequel/model/associations.rb', line 1350

def join_table_source
  cached_fetch(:join_table_source){split_join_table_alias[0]}
end

#need_associated_primary_key?Boolean

Whether the associated object needs a primary key to be added/removed, true for many_to_many associations.

Returns:

  • (Boolean)


1366
1367
1368
# File 'lib/sequel/model/associations.rb', line 1366

def need_associated_primary_key?
  true
end

#predicate_keyObject Also known as: qualified_left_key

The hash key to use for the eager loading predicate (left side of IN (1, 2, 3)). The left key qualified by the join table.



1333
1334
1335
# File 'lib/sequel/model/associations.rb', line 1333

def predicate_key
  cached_fetch(:predicate_key){qualify(join_table_alias, self[:left_key])}
end

#qualified_right_keyObject

The right key qualified by the join table.



1339
1340
1341
# File 'lib/sequel/model/associations.rb', line 1339

def qualified_right_key
  cached_fetch(:qualified_right_key){qualify(join_table_alias, self[:right_key])}
end

#qualified_right_primary_keyObject

#right_primary_key qualified by the associated table



1371
1372
1373
# File 'lib/sequel/model/associations.rb', line 1371

def qualified_right_primary_key
  cached_fetch(:qualified_right_primary_key){qualify_assoc(right_primary_key)}
end

#right_primary_keyObject

The primary key column(s) to use in the associated table (can be symbol or array).



1376
1377
1378
# File 'lib/sequel/model/associations.rb', line 1376

def right_primary_key
  cached_fetch(:right_primary_key){associated_class.primary_key || raise(Error, "no primary key specified for #{associated_class.inspect}")}
end

#right_primary_key_methodObject

The method symbol or array of method symbols to call on the associated objects to get the foreign key values for the join table.



1387
1388
1389
# File 'lib/sequel/model/associations.rb', line 1387

def right_primary_key_method
  cached_fetch(:right_primary_key_method){right_primary_key}
end

#right_primary_key_methodsObject

The array of method symbols to call on the associated objects to get the foreign key values for the join table.



1393
1394
1395
# File 'lib/sequel/model/associations.rb', line 1393

def right_primary_key_methods
  cached_fetch(:right_primary_key_methods){Array(right_primary_key_method)}
end

#right_primary_keysObject

The primary key columns to use in the associated table (always array).



1381
1382
1383
# File 'lib/sequel/model/associations.rb', line 1381

def right_primary_keys
  cached_fetch(:right_primary_keys){Array(right_primary_key)}
end

#selectObject

The columns to select when loading the association, associated_class.table_name.* by default.



1398
1399
1400
# File 'lib/sequel/model/associations.rb', line 1398

def select
  cached_fetch(:select){default_select}
end

#separate_query_per_table?Boolean

Whether a separate query should be used for the join table.

Returns:

  • (Boolean)


1403
1404
1405
# File 'lib/sequel/model/associations.rb', line 1403

def separate_query_per_table?
  self[:join_table_db]
end