Method: Sequel::Model::Associations::ClassMethods#eager_loading_dataset

Defined in:
lib/sequel/model/associations.rb

#eager_loading_dataset(opts, ds, select, associations) ⇒ Object

Modify and return eager loading dataset based on association options. Options:



650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
# File 'lib/sequel/model/associations.rb', line 650

def eager_loading_dataset(opts, ds, select, associations)
  ds = ds.select(*select) if select
  if c = opts[:conditions]
    ds = (c.is_a?(Array) && !Sequel.condition_specifier?(c)) ? ds.filter(*c) : ds.filter(c)
  end
  ds = ds.order(*opts[:order]) if opts[:order]
  ds = ds.eager(opts[:eager]) if opts[:eager]
  ds = ds.distinct if opts[:distinct]
  if opts[:eager_graph]
    ds = ds.eager_graph(opts[:eager_graph])
    ds = ds.add_graph_aliases(opts.associated_key_alias=>[opts.associated_class.table_name, opts.associated_key_alias, SQL::QualifiedIdentifier.new(opts.associated_key_table, opts.associated_key_column)]) if opts.eager_loading_use_associated_key?
  elsif opts.eager_loading_use_associated_key?
    ds = if opts[:uses_left_composite_keys]
      t = opts.associated_key_table
      ds.select_more(*opts.associated_key_alias.zip(opts.associated_key_column).map{|a, c| SQL::AliasedExpression.new(SQL::QualifiedIdentifier.new(t, c), a)})
    else
      ds.select_more(SQL::AliasedExpression.new(SQL::QualifiedIdentifier.new(opts.associated_key_table, opts.associated_key_column), opts.associated_key_alias)) 
    end
  end
  ds = ds.eager(associations) unless Array(associations).empty?
  ds = opts[:eager_block].call(ds) if opts[:eager_block]
  ds
end