Class: Sequel::Model::Associations::AssociationReflection
- Includes:
- Inflections
- Defined in:
- lib/sequel/model/associations.rb
Overview
AssociationReflection is a Hash subclass that keeps information on Sequel::Model associations. It provides methods to reduce internal code duplication. It should not be instantiated by the user.
Direct Known Subclasses
ManyToManyAssociationReflection, ManyToOneAssociationReflection, OneToManyAssociationReflection, Plugins::PgArrayAssociations::ManyToPgArrayAssociationReflection, Plugins::PgArrayAssociations::PgArrayToManyAssociationReflection
Constant Summary collapse
- ASSOCIATION_DATASET_PROC =
proc{|r| r.association_dataset_for(self)}
- FINALIZE_SETTINGS =
Map of methods to cache keys used for finalizing associations.
{ :associated_class=>:class, :associated_dataset=>:_dataset, :eager_limit_strategy=>:_eager_limit_strategy, :placeholder_loader=>:placeholder_loader, :predicate_key=>:predicate_key, :predicate_keys=>:predicate_keys, :reciprocal=>:reciprocal, }.freeze
Instance Method Summary collapse
-
#_add_method ⇒ Object
Name symbol for the _add internal association method.
-
#_remove_all_method ⇒ Object
Name symbol for the _remove_all internal association method.
-
#_remove_method ⇒ Object
Name symbol for the _remove internal association method.
-
#_setter_method ⇒ Object
Name symbol for the _setter association method.
-
#add_method ⇒ Object
Name symbol for the add association method.
-
#apply_dataset_changes(ds) ⇒ Object
Apply all non-instance specific changes to the given dataset and return it.
-
#apply_distinct_on_eager_limit_strategy(ds) ⇒ Object
Use DISTINCT ON and ORDER BY clauses to limit the results to the first record with matching keys.
-
#apply_eager_dataset_changes(ds) ⇒ Object
Apply all non-instance specific changes and the eager_block option to the given dataset and return it.
-
#apply_eager_graph_limit_strategy(strategy, ds) ⇒ Object
Apply the eager graph limit strategy to the dataset to graph into the current dataset, or return the dataset unmodified if no SQL limit strategy is needed.
-
#apply_eager_limit_strategy(ds, strategy = eager_limit_strategy, limit_and_offset = limit_and_offset()) ⇒ Object
Apply an eager limit strategy to the dataset, or return the dataset unmodified if it doesn’t need an eager limit strategy.
-
#apply_ruby_eager_limit_strategy(rows, limit_and_offset = limit_and_offset()) ⇒ Object
If the ruby eager limit strategy is being used, slice the array using the slice range to return the object(s) at the correct offset/limit.
-
#apply_window_function_eager_limit_strategy(ds, limit_and_offset = limit_and_offset()) ⇒ Object
Use a window function to limit the results of the eager loading dataset.
-
#assign_singular? ⇒ Boolean
Whether the associations cache should use an array when storing the associated records during eager loading.
-
#associated_class ⇒ Object
The class associated to the current model class via this association.
-
#associated_dataset ⇒ Object
The dataset associated via this association, with the non-instance specific changes already applied.
-
#association_dataset_for(object) ⇒ Object
Return an dataset that will load the appropriate associated objects for the given object using this association.
-
#association_dataset_proc ⇒ Object
Proc used to create the association dataset method.
-
#association_method ⇒ Object
Name symbol for association method, the same as the name of the association.
-
#can_have_associated_objects?(obj) ⇒ Boolean
Whether this association can have associated objects, given the current object.
-
#cloneable?(ref) ⇒ Boolean
Whether you are able to clone from the given association type to the current association type, true by default only if the types match.
-
#dataset_method ⇒ Object
Name symbol for the dataset association method.
-
#dataset_need_primary_key? ⇒ Boolean
Whether the dataset needs a primary key to function, true by default.
-
#delete_row_number_column(ds = associated_dataset) ⇒ Object
Return the symbol used for the row number column if the window function eager limit strategy is being used, or nil otherwise.
-
#eager_graph_lazy_dataset? ⇒ Boolean
Whether to eagerly graph a lazy dataset, true by default.
-
#eager_graph_limit_strategy(strategy) ⇒ Object
The eager_graph limit strategy to use for this dataset.
-
#eager_limit_strategy ⇒ Object
The eager limit strategy to use for this dataset.
-
#eager_load_results(eo, &block) ⇒ Object
Eager load the associated objects using the hash of eager options, yielding each row to the block.
-
#eager_loader_key ⇒ Object
The key to use for the key hash when eager loading.
-
#eager_loading_use_associated_key? ⇒ Boolean
By default associations do not need to select a key in an associated table to eagerly load.
-
#filter_by_associations_add_conditions? ⇒ Boolean
Whether additional conditions should be added when using the filter by associations support.
-
#filter_by_associations_conditions_expression(obj) ⇒ Object
The expression to use for the additional conditions to be added for the filter by association support, when the association itself is filtered.
-
#finalize ⇒ Object
Finalize the association by first attempting to populate the thread-safe cache, and then transfering the thread-safe cache value to the association itself, so that a mutex is not needed to get the value.
- #finalize_settings ⇒ Object
-
#handle_silent_modification_failure? ⇒ Boolean
Whether to handle silent modification failure when adding/removing associated records, false by default.
-
#hash ⇒ Object
Hash value for the association reflection.
-
#initialize_association_cache(objects) ⇒ Object
Initialize the associations cache for the current association for the given objects.
-
#inspect ⇒ Object
Show which type of reflection this is, and a guess at what code was used to create the association.
-
#limit_and_offset ⇒ Object
The limit and offset for this association (returned as a two element array).
-
#need_associated_primary_key? ⇒ Boolean
Whether the associated object needs a primary key to be added/removed, false by default.
-
#placeholder_loader ⇒ Object
A placeholder literalizer that can be used to lazily load the association.
-
#predicate_key_values(object) ⇒ Object
The values that predicate_keys should match for objects to be associated.
-
#predicate_keys ⇒ Object
The keys to use for loading of the regular dataset, as an array.
-
#qualify(table, col) ⇒ Object
Qualify
colwith the given table name. -
#qualify_assoc(col) ⇒ Object
Qualify col with the associated model’s table name.
-
#qualify_cur(col) ⇒ Object
Qualify col with the current model’s table name.
-
#reciprocal ⇒ Object
Returns the reciprocal association variable, if one exists.
-
#reciprocal_array? ⇒ Boolean
Whether the reciprocal of this association returns an array of objects instead of a single object, true by default.
-
#remove_all_method ⇒ Object
Name symbol for the remove_all_ association method.
-
#remove_before_destroy? ⇒ Boolean
Whether associated objects need to be removed from the association before being destroyed in order to preserve referential integrity.
-
#remove_method ⇒ Object
Name symbol for the remove_ association method.
-
#remove_should_check_existing? ⇒ Boolean
Whether to check that an object to be disassociated is already associated to this object, false by default.
-
#returns_array? ⇒ Boolean
Whether this association returns an array of objects instead of a single object, true by default.
-
#select ⇒ Object
The columns to select when loading the association.
-
#set_reciprocal_to_self? ⇒ Boolean
Whether to set the reciprocal association to self when loading associated records, false by default.
-
#setter_method ⇒ Object
Name symbol for the setter association method.
-
#slice_range(limit_and_offset = limit_and_offset()) ⇒ Object
The range used for slicing when using the :ruby eager limit strategy.
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
#_add_method ⇒ Object
Name symbol for the _add internal association method
36 37 38 |
# File 'lib/sequel/model/associations.rb', line 36 def _add_method self[:_add_method] end |
#_remove_all_method ⇒ Object
Name symbol for the _remove_all internal association method
41 42 43 |
# File 'lib/sequel/model/associations.rb', line 41 def _remove_all_method self[:_remove_all_method] end |
#_remove_method ⇒ Object
Name symbol for the _remove internal association method
46 47 48 |
# File 'lib/sequel/model/associations.rb', line 46 def _remove_method self[:_remove_method] end |
#_setter_method ⇒ Object
Name symbol for the _setter association method
51 52 53 |
# File 'lib/sequel/model/associations.rb', line 51 def _setter_method self[:_setter_method] end |
#add_method ⇒ Object
Name symbol for the add association method
56 57 58 |
# File 'lib/sequel/model/associations.rb', line 56 def add_method self[:add_method] end |
#apply_dataset_changes(ds) ⇒ Object
Apply all non-instance specific changes to the given dataset and return it.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/sequel/model/associations.rb', line 84 def apply_dataset_changes(ds) ds = ds.with_extend(AssociationDatasetMethods).clone(:association_reflection => self) if exts = self[:reverse_extend] ds = ds.with_extend(*exts) end ds = ds.select(*select) if select if c = self[:conditions] ds = (c.is_a?(Array) && !Sequel.condition_specifier?(c)) ? ds.where(*c) : ds.where(c) end ds = ds.order(*self[:order]) if self[:order] ds = ds.limit(*self[:limit]) if self[:limit] ds = ds.limit(1).skip_limit_check if limit_to_single_row? ds = ds.eager(self[:eager]) if self[:eager] ds = ds.distinct if self[:distinct] ds end |
#apply_distinct_on_eager_limit_strategy(ds) ⇒ Object
Use DISTINCT ON and ORDER BY clauses to limit the results to the first record with matching keys.
140 141 142 143 |
# File 'lib/sequel/model/associations.rb', line 140 def apply_distinct_on_eager_limit_strategy(ds) keys = predicate_key ds.distinct(*keys).order_prepend(*keys) end |
#apply_eager_dataset_changes(ds) ⇒ Object
Apply all non-instance specific changes and the eager_block option to the given dataset and return it.
103 104 105 106 107 108 109 |
# File 'lib/sequel/model/associations.rb', line 103 def apply_eager_dataset_changes(ds) ds = apply_dataset_changes(ds) if block = self[:eager_block] ds = block.call(ds) end ds end |
#apply_eager_graph_limit_strategy(strategy, ds) ⇒ Object
Apply the eager graph limit strategy to the dataset to graph into the current dataset, or return the dataset unmodified if no SQL limit strategy is needed.
113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/sequel/model/associations.rb', line 113 def apply_eager_graph_limit_strategy(strategy, ds) case strategy when :lateral_subquery apply_lateral_subquery_eager_graph_limit_strategy(ds) when :distinct_on apply_distinct_on_eager_limit_strategy(ds.order_prepend(*self[:order])) when :window_function apply_window_function_eager_limit_strategy(ds.order_prepend(*self[:order])).select(*ds.columns) else ds end end |
#apply_eager_limit_strategy(ds, strategy = eager_limit_strategy, limit_and_offset = limit_and_offset()) ⇒ Object
Apply an eager limit strategy to the dataset, or return the dataset unmodified if it doesn’t need an eager limit strategy.
128 129 130 131 132 133 134 135 136 137 |
# File 'lib/sequel/model/associations.rb', line 128 def apply_eager_limit_strategy(ds, strategy=eager_limit_strategy, limit_and_offset=limit_and_offset()) case strategy when :distinct_on apply_distinct_on_eager_limit_strategy(ds) when :window_function apply_window_function_eager_limit_strategy(ds, limit_and_offset) else ds end end |
#apply_ruby_eager_limit_strategy(rows, limit_and_offset = limit_and_offset()) ⇒ Object
If the ruby eager limit strategy is being used, slice the array using the slice range to return the object(s) at the correct offset/limit.
167 168 169 170 171 172 173 174 175 176 |
# File 'lib/sequel/model/associations.rb', line 167 def apply_ruby_eager_limit_strategy(rows, limit_and_offset = limit_and_offset()) name = self[:name] return unless range = slice_range(limit_and_offset) if returns_array? rows.each{|o| o.associations[name] = o.associations[name][range] || []} else offset = range.begin rows.each{|o| o.associations[name] = o.associations[name][offset]} end end |
#apply_window_function_eager_limit_strategy(ds, limit_and_offset = limit_and_offset()) ⇒ Object
Use a window function to limit the results of the eager loading dataset.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/sequel/model/associations.rb', line 146 def apply_window_function_eager_limit_strategy(ds, limit_and_offset=limit_and_offset()) rn = ds.row_number_column limit, offset = limit_and_offset ds = ds.unordered.select_append{|o| o.row_number.function.over(:partition=>predicate_key, :order=>ds.opts[:order]).as(rn)}.from_self ds = ds.order(rn) if ds.db.database_type == :mysql ds = if !returns_array? ds.where(rn => offset ? offset+1 : 1) elsif offset offset += 1 if limit ds.where(rn => (offset...(offset+limit))) else ds.where{SQL::Identifier.new(rn) >= offset} end else ds.where{SQL::Identifier.new(rn) <= limit} end end |
#assign_singular? ⇒ Boolean
Whether the associations cache should use an array when storing the associated records during eager loading.
180 181 182 |
# File 'lib/sequel/model/associations.rb', line 180 def assign_singular? !returns_array? end |
#associated_class ⇒ Object
The class associated to the current model class via this association
66 67 68 69 70 71 72 73 74 |
# File 'lib/sequel/model/associations.rb', line 66 def associated_class cached_fetch(:class) do begin constantize(self[:class_name]) rescue NameError => e raise NameError, "#{e.} (this happened when attempting to find the associated class for #{inspect})", e.backtrace end end end |
#associated_dataset ⇒ Object
The dataset associated via this association, with the non-instance specific changes already applied. This will be a joined dataset if the association requires joining tables.
79 80 81 |
# File 'lib/sequel/model/associations.rb', line 79 def associated_dataset cached_fetch(:_dataset){apply_dataset_changes(_associated_dataset)} end |
#association_dataset_for(object) ⇒ Object
Return an dataset that will load the appropriate associated objects for the given object using this association.
217 218 219 220 221 222 223 224 225 |
# File 'lib/sequel/model/associations.rb', line 217 def association_dataset_for(object) condition = if can_have_associated_objects?(object) predicate_keys.zip(predicate_key_values(object)) else false end associated_dataset.where(condition) end |
#association_dataset_proc ⇒ Object
Proc used to create the association dataset method.
229 230 231 |
# File 'lib/sequel/model/associations.rb', line 229 def association_dataset_proc ASSOCIATION_DATASET_PROC end |
#association_method ⇒ Object
Name symbol for association method, the same as the name of the association.
61 62 63 |
# File 'lib/sequel/model/associations.rb', line 61 def association_method self[:name] end |
#can_have_associated_objects?(obj) ⇒ Boolean
Whether this association can have associated objects, given the current object. Should be false if obj cannot have associated objects because the necessary key columns are NULL.
187 188 189 |
# File 'lib/sequel/model/associations.rb', line 187 def can_have_associated_objects?(obj) true end |
#cloneable?(ref) ⇒ Boolean
Whether you are able to clone from the given association type to the current association type, true by default only if the types match.
193 194 195 |
# File 'lib/sequel/model/associations.rb', line 193 def cloneable?(ref) ref[:type] == self[:type] end |
#dataset_method ⇒ Object
Name symbol for the dataset association method
198 199 200 |
# File 'lib/sequel/model/associations.rb', line 198 def dataset_method self[:dataset_method] end |
#dataset_need_primary_key? ⇒ Boolean
Whether the dataset needs a primary key to function, true by default.
203 204 205 |
# File 'lib/sequel/model/associations.rb', line 203 def dataset_need_primary_key? true end |
#delete_row_number_column(ds = associated_dataset) ⇒ Object
Return the symbol used for the row number column if the window function eager limit strategy is being used, or nil otherwise.
209 210 211 212 213 |
# File 'lib/sequel/model/associations.rb', line 209 def delete_row_number_column(ds=associated_dataset) if eager_limit_strategy == :window_function ds.row_number_column end end |
#eager_graph_lazy_dataset? ⇒ Boolean
Whether to eagerly graph a lazy dataset, true by default. If this is false, the association won’t respect the :eager_graph option when loading the association for a single record.
361 362 363 |
# File 'lib/sequel/model/associations.rb', line 361 def eager_graph_lazy_dataset? true end |
#eager_graph_limit_strategy(strategy) ⇒ Object
The eager_graph limit strategy to use for this dataset
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/sequel/model/associations.rb', line 234 def eager_graph_limit_strategy(strategy) if self[:limit] || !returns_array? strategy = strategy[self[:name]] if strategy.is_a?(Hash) case strategy when true true_eager_graph_limit_strategy when Symbol strategy else if returns_array? || offset :ruby end end end end |
#eager_limit_strategy ⇒ Object
The eager limit strategy to use for this dataset.
251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/sequel/model/associations.rb', line 251 def eager_limit_strategy cached_fetch(:_eager_limit_strategy) do if self[:limit] || !returns_array? case s = cached_fetch(:eager_limit_strategy){default_eager_limit_strategy} when true true_eager_limit_strategy else s end end end end |
#eager_load_results(eo, &block) ⇒ Object
Eager load the associated objects using the hash of eager options, yielding each row to the block.
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/sequel/model/associations.rb', line 266 def eager_load_results(eo, &block) rows = eo[:rows] unless eo[:initialize_rows] == false Sequel.synchronize_with(eo[:mutex]){initialize_association_cache(rows)} end if eo[:id_map] ids = eo[:id_map].keys return ids if ids.empty? end strategy = eager_limit_strategy cascade = eo[:associations] eager_limit = nil if eo[:no_results] no_results = true elsif eo[:eager_block] || eo[:loader] == false || !use_placeholder_loader? ds = eager_loading_dataset(eo) strategy = ds.opts[:eager_limit_strategy] || strategy eager_limit = if el = ds.opts[:eager_limit] raise Error, "The :eager_limit dataset option is not supported for associations returning a single record" unless returns_array? strategy ||= true_eager_graph_limit_strategy if el.is_a?(Array) el else [el, nil] end else limit_and_offset end strategy = true_eager_graph_limit_strategy if strategy == :union # Correlated subqueries are not supported for regular eager loading strategy = :ruby if strategy == :correlated_subquery strategy = nil if strategy == :ruby && assign_singular? objects = if strategy == :lateral_subquery apply_lateral_subquery_eager_limit_strategy(ds, ids, eager_limit).all else apply_eager_limit_strategy(ds, strategy, eager_limit).all end if strategy == :window_function delete_rn = ds.row_number_column objects.each{|obj| obj.remove_key!(delete_rn)} end elsif strategy == :union objects = [] ds = associated_dataset loader = union_eager_loader joiner = " UNION ALL " ids.each_slice(subqueries_per_union).each do |slice| sql = loader.send(:sql_origin) join = false slice.each do |k| if join sql << joiner else join = true end loader.append_sql(sql, *k) end objects.concat(ds.with_sql(sql.freeze).to_a) end ds = ds.eager(cascade) if cascade ds.send(:post_load, objects) else loader = placeholder_eager_loader loader = loader.with_dataset{|dataset| dataset.eager(cascade)} if cascade objects = loader.all(ids) end Sequel.synchronize_with(eo[:mutex]){objects.each(&block)} unless no_results if strategy == :ruby apply_ruby_eager_limit_strategy(rows, eager_limit || limit_and_offset) end end |
#eager_loader_key ⇒ Object
The key to use for the key hash when eager loading
348 349 350 |
# File 'lib/sequel/model/associations.rb', line 348 def eager_loader_key self[:eager_loader_key] end |
#eager_loading_use_associated_key? ⇒ Boolean
By default associations do not need to select a key in an associated table to eagerly load.
354 355 356 |
# File 'lib/sequel/model/associations.rb', line 354 def eager_loading_use_associated_key? false end |
#filter_by_associations_add_conditions? ⇒ Boolean
Whether additional conditions should be added when using the filter by associations support.
367 368 369 |
# File 'lib/sequel/model/associations.rb', line 367 def filter_by_associations_add_conditions? self[:conditions] || self[:eager_block] || self[:limit] end |
#filter_by_associations_conditions_expression(obj) ⇒ Object
The expression to use for the additional conditions to be added for the filter by association support, when the association itself is filtered. Works by using a subquery to test that the objects passed also meet the association filter criteria.
375 376 377 378 379 380 381 382 383 |
# File 'lib/sequel/model/associations.rb', line 375 def filter_by_associations_conditions_expression(obj) ds = if filter_by_associations_limit_strategy == :lateral_subquery apply_lateral_subquery_filter_limit_strategy(associated_eager_dataset, obj) else filter_by_associations_conditions_dataset.where(filter_by_associations_conditions_subquery_conditions(obj)) end {filter_by_associations_conditions_key=>ds} end |
#finalize ⇒ Object
Finalize the association by first attempting to populate the thread-safe cache, and then transfering the thread-safe cache value to the association itself, so that a mutex is not needed to get the value.
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/sequel/model/associations.rb', line 388 def finalize return unless cache = self[:cache] finalizer = proc do |meth, key| next if has_key?(key) # Allow calling private methods to make sure caching is done appropriately send(meth) self[key] = cache.delete(key) if cache.has_key?(key) end finalize_settings.each(&finalizer) unless self[:instance_specific] finalizer.call(:associated_eager_dataset, :associated_eager_dataset) finalizer.call(:filter_by_associations_conditions_dataset, :filter_by_associations_conditions_dataset) end nil end |
#finalize_settings ⇒ Object
419 420 421 |
# File 'lib/sequel/model/associations.rb', line 419 def finalize_settings FINALIZE_SETTINGS end |
#handle_silent_modification_failure? ⇒ Boolean
Whether to handle silent modification failure when adding/removing associated records, false by default.
425 426 427 |
# File 'lib/sequel/model/associations.rb', line 425 def handle_silent_modification_failure? false end |
#hash ⇒ Object
Hash value for the association reflection. This is precomputed to avoid concurrency issues at runtime.
431 432 433 |
# File 'lib/sequel/model/associations.rb', line 431 def hash self[:_hash] end |
#initialize_association_cache(objects) ⇒ Object
Initialize the associations cache for the current association for the given objects.
436 437 438 439 440 441 442 443 |
# File 'lib/sequel/model/associations.rb', line 436 def initialize_association_cache(objects) name = self[:name] if assign_singular? objects.each{|object| object.associations[name] = nil} else objects.each{|object| object.associations[name] = []} end end |
#inspect ⇒ Object
Show which type of reflection this is, and a guess at what code was used to create the association.
447 448 449 450 451 452 453 454 455 |
# File 'lib/sequel/model/associations.rb', line 447 def inspect o = self[:orig_opts].dup o.delete(:class) o.delete(:class_name) o.delete(:block) unless o[:block] o[:class] = self[:orig_class] if self[:orig_class] "#<#{self.class} #{self[:model]}.#{self[:type]} #{self[:name].inspect}#{", #{o.inspect[1...-1]}" unless o.empty?}>" end |
#limit_and_offset ⇒ Object
The limit and offset for this association (returned as a two element array).
458 459 460 461 462 463 464 |
# File 'lib/sequel/model/associations.rb', line 458 def limit_and_offset if (v = self[:limit]).is_a?(Array) v else [v, nil] end end |
#need_associated_primary_key? ⇒ Boolean
Whether the associated object needs a primary key to be added/removed, false by default.
468 469 470 |
# File 'lib/sequel/model/associations.rb', line 468 def need_associated_primary_key? false end |
#placeholder_loader ⇒ Object
A placeholder literalizer that can be used to lazily load the association. If one can’t be used, returns nil.
474 475 476 477 478 479 480 481 482 483 484 485 486 |
# File 'lib/sequel/model/associations.rb', line 474 def placeholder_loader if use_placeholder_loader? cached_fetch(:placeholder_loader) do associated_dataset.placeholder_literalizer_loader do |pl, ds| ds = ds.where(Sequel.&(*predicate_keys.map{|k| SQL::BooleanExpression.new(:'=', k, pl.arg)})) if self[:block] ds = self[:block].call(ds) end ds end end end end |
#predicate_key_values(object) ⇒ Object
The values that predicate_keys should match for objects to be associated.
494 495 496 |
# File 'lib/sequel/model/associations.rb', line 494 def predicate_key_values(object) predicate_key_methods.map{|k| object.get_column_value(k)} end |
#predicate_keys ⇒ Object
The keys to use for loading of the regular dataset, as an array.
489 490 491 |
# File 'lib/sequel/model/associations.rb', line 489 def predicate_keys cached_fetch(:predicate_keys){Array(predicate_key)} end |
#qualify(table, col) ⇒ Object
Qualify col with the given table name.
499 500 501 502 503 504 505 506 507 508 |
# File 'lib/sequel/model/associations.rb', line 499 def qualify(table, col) transform(col) do |k| case k when Symbol, SQL::Identifier SQL::QualifiedIdentifier.new(table, k) else Sequel::Qualifier.new(table).transform(k) end end end |
#qualify_assoc(col) ⇒ Object
Qualify col with the associated model’s table name.
511 512 513 |
# File 'lib/sequel/model/associations.rb', line 511 def qualify_assoc(col) qualify(associated_class.table_name, col) end |
#qualify_cur(col) ⇒ Object
Qualify col with the current model’s table name.
516 517 518 |
# File 'lib/sequel/model/associations.rb', line 516 def qualify_cur(col) qualify(self[:model].table_name, col) end |
#reciprocal ⇒ Object
Returns the reciprocal association variable, if one exists. The reciprocal association is the association in the associated class that is the opposite of the current association. For example, Album.many_to_one :artist and Artist.one_to_many :albums are reciprocal associations. This information is to populate reciprocal associations. For example, when you do this_artist.add_album(album) it sets album.artist to this_artist.
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 |
# File 'lib/sequel/model/associations.rb', line 526 def reciprocal cached_fetch(:reciprocal) do possible_recips = [] associated_class.all_association_reflections.each do |assoc_reflect| if reciprocal_association?(assoc_reflect) possible_recips << assoc_reflect end end if possible_recips.length == 1 cached_set(:reciprocal_type, possible_recips.first[:type]) if ambiguous_reciprocal_type? possible_recips.first[:name] end end end |
#reciprocal_array? ⇒ Boolean
Whether the reciprocal of this association returns an array of objects instead of a single object, true by default.
545 546 547 |
# File 'lib/sequel/model/associations.rb', line 545 def reciprocal_array? true end |
#remove_all_method ⇒ Object
Name symbol for the remove_all_ association method
550 551 552 |
# File 'lib/sequel/model/associations.rb', line 550 def remove_all_method self[:remove_all_method] end |
#remove_before_destroy? ⇒ Boolean
Whether associated objects need to be removed from the association before being destroyed in order to preserve referential integrity.
556 557 558 |
# File 'lib/sequel/model/associations.rb', line 556 def remove_before_destroy? true end |
#remove_method ⇒ Object
Name symbol for the remove_ association method
561 562 563 |
# File 'lib/sequel/model/associations.rb', line 561 def remove_method self[:remove_method] end |
#remove_should_check_existing? ⇒ Boolean
Whether to check that an object to be disassociated is already associated to this object, false by default.
566 567 568 |
# File 'lib/sequel/model/associations.rb', line 566 def remove_should_check_existing? false end |
#returns_array? ⇒ Boolean
Whether this association returns an array of objects instead of a single object, true by default.
572 573 574 |
# File 'lib/sequel/model/associations.rb', line 572 def returns_array? true end |
#select ⇒ Object
The columns to select when loading the association.
577 578 579 |
# File 'lib/sequel/model/associations.rb', line 577 def select self[:select] end |
#set_reciprocal_to_self? ⇒ Boolean
Whether to set the reciprocal association to self when loading associated records, false by default.
583 584 585 |
# File 'lib/sequel/model/associations.rb', line 583 def set_reciprocal_to_self? false end |
#setter_method ⇒ Object
Name symbol for the setter association method
588 589 590 |
# File 'lib/sequel/model/associations.rb', line 588 def setter_method self[:setter_method] end |
#slice_range(limit_and_offset = limit_and_offset()) ⇒ Object
The range used for slicing when using the :ruby eager limit strategy.
593 594 595 596 597 598 |
# File 'lib/sequel/model/associations.rb', line 593 def slice_range(limit_and_offset = limit_and_offset()) limit, offset = limit_and_offset if limit || offset (offset||0)..(limit ? (offset||0)+limit-1 : -1) end end |