class Sequel::Model::Associations::AssociationReflection

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.

Constants

ASSOCIATION_DATASET_PROC
FINALIZE_SETTINGS

Map of methods to cache keys used for finalizing associations.

Public Instance Methods

_add_method() click to toggle source

Name symbol for the _add internal association method

   # File lib/sequel/model/associations.rb
36 def _add_method
37   self[:_add_method]
38 end
_remove_all_method() click to toggle source

Name symbol for the _remove_all internal association method

   # File lib/sequel/model/associations.rb
41 def _remove_all_method
42   self[:_remove_all_method]
43 end
_remove_method() click to toggle source

Name symbol for the _remove internal association method

   # File lib/sequel/model/associations.rb
46 def _remove_method
47   self[:_remove_method]
48 end
_setter_method() click to toggle source

Name symbol for the _setter association method

   # File lib/sequel/model/associations.rb
51 def _setter_method
52   self[:_setter_method]
53 end
add_method() click to toggle source

Name symbol for the add association method

   # File lib/sequel/model/associations.rb
56 def add_method
57   self[:add_method]
58 end
apply_dataset_changes(ds) click to toggle source

Apply all non-instance specific changes to the given dataset and return it.

   # File lib/sequel/model/associations.rb
84 def apply_dataset_changes(ds)
85   ds = ds.with_extend(AssociationDatasetMethods).clone(:association_reflection => self)
86   if exts = self[:reverse_extend]
87     ds = ds.with_extend(*exts)
88   end
89   ds = ds.select(*select) if select
90   if c = self[:conditions]
91     ds = (c.is_a?(Array) && !Sequel.condition_specifier?(c)) ? ds.where(*c) : ds.where(c)
92   end
93   ds = ds.order(*self[:order]) if self[:order]
94   ds = ds.limit(*self[:limit]) if self[:limit]
95   ds = ds.limit(1).skip_limit_check if limit_to_single_row?
96   ds = ds.eager(self[:eager]) if self[:eager]
97   ds = ds.distinct if self[:distinct]
98   ds
99 end
apply_distinct_on_eager_limit_strategy(ds) click to toggle source

Use DISTINCT ON and ORDER BY clauses to limit the results to the first record with matching keys.

    # File lib/sequel/model/associations.rb
138 def apply_distinct_on_eager_limit_strategy(ds)
139   keys = predicate_key
140   ds.distinct(*keys).order_prepend(*keys)
141 end
apply_eager_dataset_changes(ds) click to toggle source

Apply all non-instance specific changes and the eager_block option to the given dataset and return it.

    # File lib/sequel/model/associations.rb
103 def apply_eager_dataset_changes(ds)
104   ds = apply_dataset_changes(ds)
105   if block = self[:eager_block]
106     ds = block.call(ds)
107   end
108   ds
109 end
apply_eager_graph_limit_strategy(strategy, ds) click to toggle source

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.

    # File lib/sequel/model/associations.rb
113 def apply_eager_graph_limit_strategy(strategy, ds)
114   case strategy
115   when :distinct_on
116     apply_distinct_on_eager_limit_strategy(ds.order_prepend(*self[:order]))
117   when :window_function
118     apply_window_function_eager_limit_strategy(ds.order_prepend(*self[:order])).select(*ds.columns)
119   else
120     ds
121   end
122 end
apply_eager_limit_strategy(ds, strategy=eager_limit_strategy, limit_and_offset=limit_and_offset()) click to toggle source

Apply an eager limit strategy to the dataset, or return the dataset unmodified if it doesn't need an eager limit strategy.

    # File lib/sequel/model/associations.rb
126 def apply_eager_limit_strategy(ds, strategy=eager_limit_strategy, limit_and_offset=limit_and_offset())
127   case strategy
128   when :distinct_on
129     apply_distinct_on_eager_limit_strategy(ds)
130   when :window_function
131     apply_window_function_eager_limit_strategy(ds, limit_and_offset)
132   else
133     ds
134   end
135 end
apply_ruby_eager_limit_strategy(rows, limit_and_offset = limit_and_offset()) click to toggle source

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.

    # File lib/sequel/model/associations.rb
165 def apply_ruby_eager_limit_strategy(rows, limit_and_offset = limit_and_offset())
166   name = self[:name]
167   return unless range = slice_range(limit_and_offset)
168   if returns_array?
169     rows.each{|o| o.associations[name] = o.associations[name][range] || []}
170   else
171     offset = range.begin
172     rows.each{|o| o.associations[name] = o.associations[name][offset]}
173   end
174 end
apply_window_function_eager_limit_strategy(ds, limit_and_offset=limit_and_offset()) click to toggle source

Use a window function to limit the results of the eager loading dataset.

    # File lib/sequel/model/associations.rb
144 def apply_window_function_eager_limit_strategy(ds, limit_and_offset=limit_and_offset())
145   rn = ds.row_number_column 
146   limit, offset = limit_and_offset
147   ds = ds.unordered.select_append{|o| o.row_number.function.over(:partition=>predicate_key, :order=>ds.opts[:order]).as(rn)}.from_self
148   ds = ds.order(rn) if ds.db.database_type == :mysql
149   ds = if !returns_array?
150     ds.where(rn => offset ? offset+1 : 1)
151   elsif offset
152     offset += 1
153     if limit
154       ds.where(rn => (offset...(offset+limit))) 
155     else
156       ds.where{SQL::Identifier.new(rn) >= offset} 
157     end
158   else
159     ds.where{SQL::Identifier.new(rn) <= limit} 
160   end
161 end
assign_singular?() click to toggle source

Whether the associations cache should use an array when storing the associated records during eager loading.

    # File lib/sequel/model/associations.rb
178 def assign_singular?
179   !returns_array?
180 end
associated_class() click to toggle source

The class associated to the current model class via this association

   # File lib/sequel/model/associations.rb
66 def associated_class
67   cached_fetch(:class) do
68     begin
69       constantize(self[:class_name])
70     rescue NameError => e
71       raise NameError, "#{e.message} (this happened when attempting to find the associated class for #{inspect})", e.backtrace
72     end
73   end
74 end
associated_dataset() click to toggle source

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.

   # File lib/sequel/model/associations.rb
79 def associated_dataset
80   cached_fetch(:_dataset){apply_dataset_changes(_associated_dataset)}
81 end
association_dataset_for(object) click to toggle source

Return an dataset that will load the appropriate associated objects for the given object using this association.

    # File lib/sequel/model/associations.rb
215 def association_dataset_for(object)
216   condition = if can_have_associated_objects?(object)
217     predicate_keys.zip(predicate_key_values(object))
218   else
219     false
220   end
221 
222   associated_dataset.where(condition)
223 end
association_dataset_proc() click to toggle source

Proc used to create the association dataset method.

    # File lib/sequel/model/associations.rb
227 def association_dataset_proc
228   ASSOCIATION_DATASET_PROC
229 end
association_method() click to toggle source

Name symbol for association method, the same as the name of the association.

   # File lib/sequel/model/associations.rb
61 def association_method
62   self[:name]
63 end
can_have_associated_objects?(obj) click to toggle source

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.

    # File lib/sequel/model/associations.rb
185 def can_have_associated_objects?(obj)
186   true
187 end
cloneable?(ref) click to toggle source

Whether you are able to clone from the given association type to the current association type, true by default only if the types match.

    # File lib/sequel/model/associations.rb
191 def cloneable?(ref)
192   ref[:type] == self[:type]
193 end
dataset_method() click to toggle source

Name symbol for the dataset association method

    # File lib/sequel/model/associations.rb
196 def dataset_method
197   self[:dataset_method]
198 end
dataset_need_primary_key?() click to toggle source

Whether the dataset needs a primary key to function, true by default.

    # File lib/sequel/model/associations.rb
201 def dataset_need_primary_key?
202   true
203 end
delete_row_number_column(ds=associated_dataset) click to toggle source

Return the symbol used for the row number column if the window function eager limit strategy is being used, or nil otherwise.

    # File lib/sequel/model/associations.rb
207 def delete_row_number_column(ds=associated_dataset)
208   if eager_limit_strategy == :window_function
209     ds.row_number_column 
210   end
211 end
eager_graph_lazy_dataset?() click to toggle source

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.

    # File lib/sequel/model/associations.rb
354 def eager_graph_lazy_dataset?
355   true
356 end
eager_graph_limit_strategy(strategy) click to toggle source

The eager_graph limit strategy to use for this dataset

    # File lib/sequel/model/associations.rb
232 def eager_graph_limit_strategy(strategy)
233   if self[:limit] || !returns_array?
234     strategy = strategy[self[:name]] if strategy.is_a?(Hash)
235     case strategy
236     when true
237       true_eager_graph_limit_strategy
238     when Symbol
239       strategy
240     else
241       if returns_array? || offset
242         :ruby
243       end
244     end
245   end
246 end
eager_limit_strategy() click to toggle source

The eager limit strategy to use for this dataset.

    # File lib/sequel/model/associations.rb
249 def eager_limit_strategy
250   cached_fetch(:_eager_limit_strategy) do
251     if self[:limit] || !returns_array?
252       case s = cached_fetch(:eager_limit_strategy){default_eager_limit_strategy}
253       when true
254         true_eager_limit_strategy
255       else
256         s
257       end
258     end
259   end
260 end
eager_load_results(eo, &block) click to toggle source

Eager load the associated objects using the hash of eager options, yielding each row to the block.

    # File lib/sequel/model/associations.rb
264 def eager_load_results(eo, &block)
265   rows = eo[:rows]
266   unless eo[:initialize_rows] == false
267     Sequel.synchronize_with(eo[:mutex]){initialize_association_cache(rows)}
268   end
269   if eo[:id_map]
270     ids = eo[:id_map].keys
271     return ids if ids.empty?
272   end
273   strategy = eager_limit_strategy
274   cascade = eo[:associations]
275   eager_limit = nil
276 
277   if eo[:no_results]
278     no_results = true
279   elsif eo[:eager_block] || eo[:loader] == false || !use_placeholder_loader?
280     ds = eager_loading_dataset(eo)
281 
282     strategy = ds.opts[:eager_limit_strategy] || strategy
283 
284     eager_limit =
285       if el = ds.opts[:eager_limit]
286         raise Error, "The :eager_limit dataset option is not supported for associations returning a single record" unless returns_array?
287         strategy ||= true_eager_graph_limit_strategy
288         if el.is_a?(Array)
289           el
290         else
291           [el, nil]
292         end
293       else
294         limit_and_offset
295       end
296 
297     strategy = true_eager_graph_limit_strategy if strategy == :union
298     # Correlated subqueries are not supported for regular eager loading
299     strategy = :ruby if strategy == :correlated_subquery
300     strategy = nil if strategy == :ruby && assign_singular?
301     objects = apply_eager_limit_strategy(ds, strategy, eager_limit).all
302 
303     if strategy == :window_function
304       delete_rn = ds.row_number_column 
305       objects.each{|obj| obj.values.delete(delete_rn)}
306     end
307   elsif strategy == :union
308     objects = []
309     ds = associated_dataset
310     loader = union_eager_loader
311     joiner = " UNION ALL "
312     ids.each_slice(subqueries_per_union).each do |slice|
313       sql = loader.send(:sql_origin)
314       join = false
315       slice.each do |k|
316         if join
317           sql << joiner
318         else
319           join = true
320         end
321         loader.append_sql(sql, *k)
322       end
323       objects.concat(ds.with_sql(sql).to_a)
324     end
325     ds = ds.eager(cascade) if cascade
326     ds.send(:post_load, objects)
327   else
328     loader = placeholder_eager_loader
329     loader = loader.with_dataset{|dataset| dataset.eager(cascade)} if cascade
330     objects = loader.all(ids)
331   end
332 
333   Sequel.synchronize_with(eo[:mutex]){objects.each(&block)} unless no_results
334 
335   if strategy == :ruby
336     apply_ruby_eager_limit_strategy(rows, eager_limit || limit_and_offset)
337   end
338 end
eager_loader_key() click to toggle source

The key to use for the key hash when eager loading

    # File lib/sequel/model/associations.rb
341 def eager_loader_key
342   self[:eager_loader_key]
343 end
eager_loading_use_associated_key?() click to toggle source

By default associations do not need to select a key in an associated table to eagerly load.

    # File lib/sequel/model/associations.rb
347 def eager_loading_use_associated_key?
348   false
349 end
filter_by_associations_add_conditions?() click to toggle source

Whether additional conditions should be added when using the filter by associations support.

    # File lib/sequel/model/associations.rb
360 def filter_by_associations_add_conditions?
361   self[:conditions] || self[:eager_block] || self[:limit]
362 end
filter_by_associations_conditions_expression(obj) click to toggle source

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.

    # File lib/sequel/model/associations.rb
368 def filter_by_associations_conditions_expression(obj)
369   ds = filter_by_associations_conditions_dataset.where(filter_by_associations_conditions_subquery_conditions(obj))
370   {filter_by_associations_conditions_key=>ds}
371 end
finalize() click to toggle source

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.

    # File lib/sequel/model/associations.rb
376 def finalize
377   return unless cache = self[:cache]
378 
379   finalizer = proc do |meth, key|
380     next if has_key?(key)
381 
382     # Allow calling private methods to make sure caching is done appropriately
383     send(meth)
384     self[key] = cache.delete(key) if cache.has_key?(key)
385   end
386 
387   finalize_settings.each(&finalizer)
388 
389   unless self[:instance_specific]
390     finalizer.call(:associated_eager_dataset, :associated_eager_dataset)
391     finalizer.call(:filter_by_associations_conditions_dataset, :filter_by_associations_conditions_dataset)
392   end
393 
394   nil
395 end
finalize_settings() click to toggle source
    # File lib/sequel/model/associations.rb
407 def finalize_settings
408   FINALIZE_SETTINGS
409 end
handle_silent_modification_failure?() click to toggle source

Whether to handle silent modification failure when adding/removing associated records, false by default.

    # File lib/sequel/model/associations.rb
413 def handle_silent_modification_failure?
414   false
415 end
hash() click to toggle source

Hash value for the association reflection. This is precomputed to avoid concurrency issues at runtime.

    # File lib/sequel/model/associations.rb
419 def hash
420   self[:_hash]
421 end
initialize_association_cache(objects) click to toggle source

Initialize the associations cache for the current association for the given objects.

    # File lib/sequel/model/associations.rb
424 def initialize_association_cache(objects)
425   name = self[:name]
426   if assign_singular?
427     objects.each{|object| object.associations[name] = nil}
428   else
429     objects.each{|object| object.associations[name] = []}
430   end
431 end
inspect() click to toggle source

Show which type of reflection this is, and a guess at what code was used to create the association.

    # File lib/sequel/model/associations.rb
435 def inspect
436   o = self[:orig_opts].dup
437   o.delete(:class)
438   o.delete(:class_name)
439   o.delete(:block) unless o[:block]
440   o[:class] = self[:orig_class] if self[:orig_class]
441 
442   "#<#{self.class} #{self[:model]}.#{self[:type]} #{self[:name].inspect}#{", #{o.inspect[1...-1]}" unless o.empty?}>"
443 end
limit_and_offset() click to toggle source

The limit and offset for this association (returned as a two element array).

    # File lib/sequel/model/associations.rb
446 def limit_and_offset
447   if (v = self[:limit]).is_a?(Array)
448     v
449   else
450     [v, nil]
451   end
452 end
need_associated_primary_key?() click to toggle source

Whether the associated object needs a primary key to be added/removed, false by default.

    # File lib/sequel/model/associations.rb
456 def need_associated_primary_key?
457   false
458 end
placeholder_loader() click to toggle source

A placeholder literalizer that can be used to lazily load the association. If one can't be used, returns nil.

    # File lib/sequel/model/associations.rb
462 def placeholder_loader
463   if use_placeholder_loader?
464     cached_fetch(:placeholder_loader) do
465       associated_dataset.placeholder_literalizer_loader do |pl, ds|
466         ds = ds.where(Sequel.&(*predicate_keys.map{|k| SQL::BooleanExpression.new(:'=', k, pl.arg)}))
467         if self[:block]
468           ds = self[:block].call(ds)
469         end
470         ds
471       end
472     end
473   end
474 end
predicate_key_values(object) click to toggle source

The values that predicate_keys should match for objects to be associated.

    # File lib/sequel/model/associations.rb
482 def predicate_key_values(object)
483   predicate_key_methods.map{|k| object.get_column_value(k)}
484 end
predicate_keys() click to toggle source

The keys to use for loading of the regular dataset, as an array.

    # File lib/sequel/model/associations.rb
477 def predicate_keys
478   cached_fetch(:predicate_keys){Array(predicate_key)}
479 end
qualify(table, col) click to toggle source

Qualify col with the given table name.

    # File lib/sequel/model/associations.rb
487 def qualify(table, col)
488   transform(col) do |k|
489     case k
490     when Symbol, SQL::Identifier
491       SQL::QualifiedIdentifier.new(table, k)
492     else
493       Sequel::Qualifier.new(table).transform(k)
494     end
495   end
496 end
qualify_assoc(col) click to toggle source

Qualify col with the associated model's table name.

    # File lib/sequel/model/associations.rb
499 def qualify_assoc(col)
500   qualify(associated_class.table_name, col)
501 end
qualify_cur(col) click to toggle source

Qualify col with the current model's table name.

    # File lib/sequel/model/associations.rb
504 def qualify_cur(col)
505   qualify(self[:model].table_name, col)
506 end
reciprocal() click to toggle source

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.

    # File lib/sequel/model/associations.rb
514 def reciprocal
515   cached_fetch(:reciprocal) do
516     possible_recips = []
517 
518     associated_class.all_association_reflections.each do |assoc_reflect|
519       if reciprocal_association?(assoc_reflect)
520         possible_recips << assoc_reflect
521       end
522     end
523 
524     if possible_recips.length == 1
525       cached_set(:reciprocal_type, possible_recips.first[:type]) if ambiguous_reciprocal_type?
526       possible_recips.first[:name]
527     end
528   end
529 end
reciprocal_array?() click to toggle source

Whether the reciprocal of this association returns an array of objects instead of a single object, true by default.

    # File lib/sequel/model/associations.rb
533 def reciprocal_array?
534   true
535 end
remove_all_method() click to toggle source

Name symbol for the remove_all_ association method

    # File lib/sequel/model/associations.rb
538 def remove_all_method
539   self[:remove_all_method]
540 end
remove_before_destroy?() click to toggle source

Whether associated objects need to be removed from the association before being destroyed in order to preserve referential integrity.

    # File lib/sequel/model/associations.rb
544 def remove_before_destroy?
545   true
546 end
remove_method() click to toggle source

Name symbol for the remove_ association method

    # File lib/sequel/model/associations.rb
549 def remove_method
550   self[:remove_method]
551 end
remove_should_check_existing?() click to toggle source

Whether to check that an object to be disassociated is already associated to this object, false by default.

    # File lib/sequel/model/associations.rb
554 def remove_should_check_existing?
555   false
556 end
returns_array?() click to toggle source

Whether this association returns an array of objects instead of a single object, true by default.

    # File lib/sequel/model/associations.rb
560 def returns_array?
561   true
562 end
select() click to toggle source

The columns to select when loading the association.

    # File lib/sequel/model/associations.rb
565 def select
566   self[:select]
567 end
set_reciprocal_to_self?() click to toggle source

Whether to set the reciprocal association to self when loading associated records, false by default.

    # File lib/sequel/model/associations.rb
571 def set_reciprocal_to_self?
572   false
573 end
setter_method() click to toggle source

Name symbol for the setter association method

    # File lib/sequel/model/associations.rb
576 def setter_method
577   self[:setter_method]
578 end
slice_range(limit_and_offset = limit_and_offset()) click to toggle source

The range used for slicing when using the :ruby eager limit strategy.

    # File lib/sequel/model/associations.rb
581 def slice_range(limit_and_offset = limit_and_offset())
582   limit, offset = limit_and_offset
583   if limit || offset
584     (offset||0)..(limit ? (offset||0)+limit-1 : -1)
585   end
586 end

Private Instance Methods

_associated_dataset() click to toggle source

The base dataset used for the association, before any order/conditions options have been applied.

    # File lib/sequel/model/associations.rb
611 def _associated_dataset
612   associated_class.dataset
613 end
ambiguous_reciprocal_type?() click to toggle source

Whether for the reciprocal type for the given association cannot be known in advantage, false by default.

    # File lib/sequel/model/associations.rb
617 def ambiguous_reciprocal_type?
618   false
619 end
apply_filter_by_associations_distinct_on_limit_strategy(ds) click to toggle source

Apply a distinct on eager limit strategy using IN with a subquery that uses DISTINCT ON to ensure only the first matching record for each key is included.

    # File lib/sequel/model/associations.rb
637 def apply_filter_by_associations_distinct_on_limit_strategy(ds)
638   k = filter_by_associations_limit_key 
639   ds.where(k=>apply_distinct_on_eager_limit_strategy(associated_eager_dataset.select(*k)))
640 end
apply_filter_by_associations_limit_strategy(ds) click to toggle source

Apply a limit strategy to the given dataset so that filter by associations works with a limited dataset.

    # File lib/sequel/model/associations.rb
623 def apply_filter_by_associations_limit_strategy(ds)
624   case filter_by_associations_limit_strategy
625   when :distinct_on
626     apply_filter_by_associations_distinct_on_limit_strategy(ds)
627   when :window_function
628     apply_filter_by_associations_window_function_limit_strategy(ds)
629   else
630     ds
631   end
632 end
apply_filter_by_associations_window_function_limit_strategy(ds) click to toggle source

Apply a distinct on eager limit strategy using IN with a subquery that uses a filter on the row_number window function to ensure that only rows inside the limit are returned.

    # File lib/sequel/model/associations.rb
645 def apply_filter_by_associations_window_function_limit_strategy(ds)
646   ds.where(filter_by_associations_limit_key=>apply_window_function_eager_limit_strategy(associated_eager_dataset.select(*filter_by_associations_limit_alias_key)).select(*filter_by_associations_limit_aliases))
647 end
associated_eager_dataset() click to toggle source

The associated_dataset with the eager_block callback already applied.

    # File lib/sequel/model/associations.rb
650 def associated_eager_dataset
651   cached_fetch(:associated_eager_dataset) do
652     ds = associated_dataset.unlimited
653     if block = self[:eager_block]
654       ds = block.call(ds)
655     end
656     ds
657   end
658 end
cached_fetch(key) { || ... } click to toggle source

If the key exists in the reflection hash, return it. If the key doesn't exist and association reflections are uncached, then yield to get the value. If the key doesn't exist and association reflection are cached, check the cache and return the value if present, or yield to get the value, cache the value, and return it.

    # File lib/sequel/model/associations.rb
594 def cached_fetch(key)
595   fetch(key) do
596     return yield unless h = self[:cache]
597     Sequel.synchronize{return h[key] if h.has_key?(key)}
598     value = yield
599     Sequel.synchronize{h[key] = value}
600   end
601 end
cached_set(key, value) click to toggle source

Cache the value at the given key if caching.

    # File lib/sequel/model/associations.rb
604 def cached_set(key, value)
605   return unless h = self[:cache]
606   Sequel.synchronize{h[key] = value}
607 end
default_eager_limit_strategy() click to toggle source

The default eager limit strategy to use for this association

    # File lib/sequel/model/associations.rb
687 def default_eager_limit_strategy
688   self[:model].default_eager_limit_strategy || :ruby
689 end
eager_loading_dataset(eo=OPTS) click to toggle source

The dataset to use for eager loading associated objects for multiple current objects, given the hash passed to the eager loader.

    # File lib/sequel/model/associations.rb
662 def eager_loading_dataset(eo=OPTS)
663   ds = eo[:dataset] || associated_eager_dataset
664   ds = eager_loading_set_predicate_condition(ds, eo)
665   if associations = eo[:associations]
666     ds = ds.eager(associations)
667   end
668   if block = eo[:eager_block]
669     orig_ds = ds
670     ds = block.call(ds)
671   end
672   if eager_loading_use_associated_key?
673     ds = if ds.opts[:eager_graph] && !orig_ds.opts[:eager_graph]
674       block.call(orig_ds.select_append(*associated_key_array))
675     else
676       ds.select_append(*associated_key_array)
677     end
678   end
679   if self[:eager_graph]
680     raise(Error, "cannot eagerly load a #{self[:type]} association that uses :eager_graph") if eager_loading_use_associated_key?
681     ds = ds.eager_graph(self[:eager_graph])
682   end
683   ds
684 end
eager_loading_predicate_condition(keys) click to toggle source

The predicate condition to use for the eager_loader.

    # File lib/sequel/model/associations.rb
701 def eager_loading_predicate_condition(keys)
702   if transform = self[:eager_loading_predicate_transform]
703     keys = transform.call(keys, self)
704   end
705   {predicate_key=>keys}
706 end
eager_loading_set_predicate_condition(ds, eo) click to toggle source

Set the predicate condition for the eager loading dataset based on the id map in the eager loading options.

    # File lib/sequel/model/associations.rb
693 def eager_loading_set_predicate_condition(ds, eo)
694   if id_map = eo[:id_map]
695     ds = ds.where(eager_loading_predicate_condition(id_map.keys))
696   end
697   ds
698 end
filter_by_associations_add_conditions_dataset_filter(ds) click to toggle source

Add conditions to the dataset to not include NULL values for the associated keys, and select those keys.

    # File lib/sequel/model/associations.rb
710 def filter_by_associations_add_conditions_dataset_filter(ds)
711   k = filter_by_associations_conditions_associated_keys
712   ds.select(*k).where(Sequel.negate(k.zip([])))
713 end
filter_by_associations_conditions_dataset() click to toggle source

The base dataset to use for the filter by associations conditions subquery, regardless of the objects that are passed in as filter values.

    # File lib/sequel/model/associations.rb
733 def filter_by_associations_conditions_dataset
734   cached_fetch(:filter_by_associations_conditions_dataset) do
735     ds = associated_eager_dataset.unordered
736     ds = filter_by_associations_add_conditions_dataset_filter(ds)
737     ds = apply_filter_by_associations_limit_strategy(ds)
738     ds
739   end
740 end
filter_by_associations_conditions_subquery_conditions(obj) click to toggle source

The conditions to add to the filter by associations conditions subquery to restrict it to to the object(s) that was used as the filter value.

    # File lib/sequel/model/associations.rb
718 def filter_by_associations_conditions_subquery_conditions(obj)
719   key = qualify(associated_class.table_name, associated_class.primary_key)
720   case obj
721   when Array
722     {key=>obj.map(&:pk)}
723   when Sequel::Dataset
724     {key=>obj.select(*Array(qualify(associated_class.table_name, associated_class.primary_key)))}
725   else
726     Array(key).zip(Array(obj.pk))
727   end
728 end
filter_by_associations_limit_strategy() click to toggle source

The strategy to use to filter by a limited association

    # File lib/sequel/model/associations.rb
743 def filter_by_associations_limit_strategy
744   v = fetch(:filter_limit_strategy, self[:eager_limit_strategy])
745   if v || self[:limit] || !returns_array?
746     case v ||= self[:model].default_eager_limit_strategy
747     when true, :union, :ruby
748       # Can't use a union or ruby-based strategy for filtering by associations, switch to default eager graph limit
749       # strategy.
750       true_eager_graph_limit_strategy
751     when Symbol
752       v
753     end
754   end
755 end
limit_to_single_row?() click to toggle source

Whether to limit the associated dataset to a single row.

    # File lib/sequel/model/associations.rb
758 def limit_to_single_row?
759   !returns_array?
760 end
offset() click to toggle source

Any offset to use for this association (or nil if there is no offset).

    # File lib/sequel/model/associations.rb
763 def offset
764   limit_and_offset.last
765 end
placeholder_eager_loader() click to toggle source

A placeholder literalizer used to speed up eager loading.

    # File lib/sequel/model/associations.rb
768 def placeholder_eager_loader
769   cached_fetch(:placeholder_eager_loader) do
770     eager_loading_dataset.placeholder_literalizer_loader do |pl, ds|
771       arg = pl.arg
772 
773       if transform = self[:eager_loading_predicate_transform]
774         arg = arg.transform do |v|
775           transform.call(v, self)
776         end
777       end
778 
779       apply_eager_limit_strategy(ds.where(predicate_key=>arg), eager_limit_strategy)
780     end
781   end
782 end
possible_reciprocal_types() click to toggle source

The reciprocal type as an array, should be overridden in reflection subclasses that have ambiguous reciprocal types.

    # File lib/sequel/model/associations.rb
786 def possible_reciprocal_types
787   [reciprocal_type]
788 end
reciprocal_association?(assoc_reflect) click to toggle source

Whether the given association reflection is possible reciprocal association for the current association reflection.

    # File lib/sequel/model/associations.rb
792 def reciprocal_association?(assoc_reflect)
793   possible_reciprocal_types.include?(assoc_reflect[:type]) &&
794     (begin; assoc_reflect.associated_class; rescue NameError; end) == self[:model] &&
795     assoc_reflect[:conditions].nil? &&
796     assoc_reflect[:block].nil?
797 end
subqueries_per_union() click to toggle source

The number of subqueries to use in each union query, used to eagerly load limited associations. Defaults to 40, the optimal number depends on the latency between the database and the application.

    # File lib/sequel/model/associations.rb
802 def subqueries_per_union
803   self[:subqueries_per_union] || 40
804 end
transform(s) { |s)| ... } click to toggle source

If s is an array, map s over the block. Otherwise, just call the block with s.

    # File lib/sequel/model/associations.rb
808 def transform(s, &block)
809   s.is_a?(Array) ? s.map(&block) : (yield s)
810 end
true_eager_graph_limit_strategy() click to toggle source

The eager_graph limit strategy used when true is given as the value, choosing the best strategy based on what the database supports.

    # File lib/sequel/model/associations.rb
826 def true_eager_graph_limit_strategy
827   if associated_class.dataset.supports_window_functions?
828     :window_function
829   else
830     :ruby
831   end
832 end
true_eager_limit_strategy() click to toggle source

What eager limit strategy should be used when true is given as the value, defaults to UNION as that is the fastest strategy if the appropriate keys are indexed.

    # File lib/sequel/model/associations.rb
814 def true_eager_limit_strategy
815   if self[:eager_graph] || (offset && !associated_dataset.supports_offsets_in_correlated_subqueries?)
816     # An SQL-based approach won't work if you are also eager graphing,
817     # so use a ruby based approach in that case.
818     :ruby
819   else
820     :union 
821   end
822 end
union_eager_loader() click to toggle source

A placeholder literalizer used to speed up the creation of union queries when eager loading a limited association.

    # File lib/sequel/model/associations.rb
836 def union_eager_loader
837   cached_fetch(:union_eager_loader) do
838     associated_dataset.placeholder_literalizer_loader do |pl, ds|
839       ds = self[:eager_block].call(ds) if self[:eager_block]
840       keys = predicate_keys
841       ds = ds.where(keys.map{pl.arg}.zip(keys))
842       if eager_loading_use_associated_key?
843         ds = ds.select_append(*associated_key_array)
844       end
845       ds.from_self
846     end
847   end
848 end
use_placeholder_loader?() click to toggle source

Whether the placeholder loader can be used to load the association.

    # File lib/sequel/model/associations.rb
851 def use_placeholder_loader?
852   self[:use_placeholder_loader] && _associated_dataset.supports_placeholder_literalizer?
853 end