Class Sequel::Model::Associations::ManyToManyAssociationReflection
In: lib/sequel/model/associations.rb
Parent: AssociationReflection

Methods

Constants

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

Public Instance methods

The alias to use for the associated key when eagerly loading

[Source]

      # File lib/sequel/model/associations.rb, line 1182
1182:         def associated_key_alias
1183:           self[:left_key_alias]
1184:         end

Array of associated keys used when eagerly loading.

[Source]

      # File lib/sequel/model/associations.rb, line 1187
1187:         def associated_key_array
1188:           cached_fetch(:associated_key_array) do
1189:             if self[:uses_left_composite_keys]
1190:               associated_key_alias.zip(predicate_keys).map{|a, k| SQL::AliasedExpression.new(k, a)}
1191:             else
1192:               [SQL::AliasedExpression.new(predicate_key, associated_key_alias)]
1193:             end
1194:           end
1195:         end

The column to use for the associated key when eagerly loading

[Source]

      # File lib/sequel/model/associations.rb, line 1198
1198:         def associated_key_column
1199:           self[:left_key]
1200:         end
associated_key_table()

Alias for join_table_alias

Alias of right_primary_keys

[Source]

      # File lib/sequel/model/associations.rb, line 1203
1203:         def associated_object_keys
1204:           right_primary_keys
1205:         end

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

[Source]

      # File lib/sequel/model/associations.rb, line 1209
1209:         def can_have_associated_objects?(obj)
1210:           !self[:left_primary_keys].any?{|k| obj.get_column_value(k).nil?}
1211:         end

one_through_one and many_to_many associations can be clones

[Source]

      # File lib/sequel/model/associations.rb, line 1214
1214:         def cloneable?(ref)
1215:           ref[:type] == :many_to_many || ref[:type] == :one_through_one
1216:         end

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

[Source]

      # File lib/sequel/model/associations.rb, line 1220
1220:         def default_associated_key_alias
1221:           self[:uses_left_composite_keys] ? (0...self[:left_keys].length).map{|i| "x_foreign_key_#{i}_x""x_foreign_key_#{i}_x"} : :x_foreign_key_x
1222:         end

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.

[Source]

      # File lib/sequel/model/associations.rb, line 1226
1226:         def default_eager_loader(eo)
1227:           h = eo[:id_map]
1228:           assign_singular = assign_singular?
1229:           delete_rn = delete_row_number_column
1230:           uses_lcks = self[:uses_left_composite_keys]
1231:           left_key_alias = self[:left_key_alias]
1232:           name = self[:name]
1233: 
1234:           self[:model].eager_load_results(self, eo) do |assoc_record|
1235:             assoc_record.values.delete(delete_rn) if delete_rn
1236:             hash_key = if uses_lcks
1237:               left_key_alias.map{|k| assoc_record.values.delete(k)}
1238:             else
1239:               assoc_record.values.delete(left_key_alias)
1240:             end
1241:             next unless objects = h[hash_key]
1242:             if assign_singular
1243:               objects.each do |object| 
1244:                 object.associations[name] ||= assoc_record
1245:               end
1246:             else
1247:               objects.each do |object|
1248:                 object.associations[name].push(assoc_record)
1249:               end
1250:             end
1251:           end
1252:         end

Default name symbol for the join table.

[Source]

      # File lib/sequel/model/associations.rb, line 1255
1255:         def default_join_table
1256:           [self[:class_name], self[:model].name].map{|i| underscore(pluralize(demodulize(i)))}.sort.join('_').to_sym
1257:         end

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

[Source]

      # File lib/sequel/model/associations.rb, line 1261
1261:         def default_left_key
1262: 
1263:           "#{underscore(demodulize(self[:model].name))}_id"
1264:         end

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).

[Source]

      # File lib/sequel/model/associations.rb, line 1267
1267:         def default_right_key
1268: 
1269:           "#{singularize(self[:name])}_id"
1270:         end

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

[Source]

      # File lib/sequel/model/associations.rb, line 1300
1300:         def eager_loading_use_associated_key?
1301:           true
1302:         end

[Source]

      # File lib/sequel/model/associations.rb, line 1283
1283:         def finalize_settings
1284:           FINALIZE_SETTINGS
1285:         end

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

[Source]

      # File lib/sequel/model/associations.rb, line 1312
1312:         def join_table_alias
1313:           cached_fetch(:join_table_alias) do
1314:             s, a = split_join_table_alias
1315:             a || s
1316:           end
1317:         end

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

[Source]

      # File lib/sequel/model/associations.rb, line 1306
1306:         def join_table_source
1307:           cached_fetch(:join_table_source){split_join_table_alias[0]}
1308:         end

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

[Source]

      # File lib/sequel/model/associations.rb, line 1322
1322:         def need_associated_primary_key?
1323:           true
1324:         end

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.

[Source]

      # File lib/sequel/model/associations.rb, line 1289
1289:         def predicate_key
1290:           cached_fetch(:predicate_key){qualify(join_table_alias, self[:left_key])}
1291:         end
qualified_left_key()

Alias for predicate_key

The right key qualified by the join table.

[Source]

      # File lib/sequel/model/associations.rb, line 1295
1295:         def qualified_right_key
1296:           cached_fetch(:qualified_right_key){qualify(join_table_alias, self[:right_key])}
1297:         end

right_primary_key qualified by the associated table

[Source]

      # File lib/sequel/model/associations.rb, line 1327
1327:         def qualified_right_primary_key
1328:           cached_fetch(:qualified_right_primary_key){qualify_assoc(right_primary_key)}
1329:         end

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

[Source]

      # File lib/sequel/model/associations.rb, line 1332
1332:         def right_primary_key
1333:           cached_fetch(:right_primary_key){associated_class.primary_key || raise(Error, "no primary key specified for #{associated_class.inspect}")}
1334:         end

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

[Source]

      # File lib/sequel/model/associations.rb, line 1343
1343:         def right_primary_key_method
1344:           cached_fetch(:right_primary_key_method){right_primary_key}
1345:         end

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

[Source]

      # File lib/sequel/model/associations.rb, line 1349
1349:         def right_primary_key_methods
1350:           cached_fetch(:right_primary_key_methods){Array(right_primary_key_method)}
1351:         end

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

[Source]

      # File lib/sequel/model/associations.rb, line 1337
1337:         def right_primary_keys
1338:           cached_fetch(:right_primary_keys){Array(right_primary_key)}
1339:         end

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

[Source]

      # File lib/sequel/model/associations.rb, line 1354
1354:         def select
1355:           cached_fetch(:select){default_select}
1356:         end

[Validate]