module Sequel::Plugins::DatasetAssociations::DatasetMethods
Public Instance Methods
associated(name)
click to toggle source
For the association given by name
, return a dataset of
associated objects such that it would return the union of calling the
association method on all objects returned by the current dataset.
This supports most options that are supported when eager loading. However, it will only work for limited associations or *_one associations with orders if the database supports window functions.
# File lib/sequel/plugins/dataset_associations.rb, line 80 def associated(name) raise Error, "unrecognized association name: #{name.inspect}" unless r = model.association_reflection(name) ds = r.associated_class.dataset sds = opts[:limit] ? self : unordered ds = case r[:type] when :many_to_one ds.where(r.qualified_primary_key=>sds.select(*Array(r[:qualified_key]))) when :one_to_one, :one_to_many r.send(:apply_filter_by_associations_limit_strategy, ds.where(r.qualified_key=>sds.select(*Array(r.qualified_primary_key)))) when :many_to_many, :one_through_one mds = r.associated_class.dataset. join(r[:join_table], r[:right_keys].zip(r.right_primary_keys)). select(*Array(r.qualified_right_key)). where(r.qualify(r.join_table_alias, r[:left_keys])=>sds.select(*r.qualify(model.table_name, r[:left_primary_key_columns]))) ds.where(r.qualified_right_primary_key=>r.send(:apply_filter_by_associations_limit_strategy, mds)) when :many_through_many, :one_through_many fe, *edges = r.edges edges << r.final_edge if fre = r.reverse_edges.first table = fre[:table] left = fre[:left] mds = model.join(fe[:table], Array(fe[:right]).zip(Array(fe[:left])), :implicit_qualifier=>model.table_name) else table = fe[:table] left = edges.first[:left] edges = [] mds = r.associated_dataset end mds = mds. select(*Array(r.qualify(table, left))). where(r.qualify(fe[:table], fe[:right])=>sds.select(*r.qualify(model.table_name, r[:left_primary_key_columns]))) edges.each{|e| mds = mds.join(e[:table], Array(e[:right]).zip(Array(e[:left])))} ds.where(r.qualified_right_primary_key=>r.send(:apply_filter_by_associations_limit_strategy, mds)) when :pg_array_to_many ds.where(Sequel[r.primary_key=>sds.select{Sequel.pg_array_op(r.qualify(r[:model].table_name, r[:key])).unnest}]) when :many_to_pg_array ds.where(Sequel.function(:coalesce, Sequel.pg_array_op(r[:key]).overlaps(sds.select{array_agg(r.qualify(r[:model].table_name, r.primary_key))}), false)) else raise Error, "unrecognized association type for association #{name.inspect}: #{r[:type].inspect}" end ds = r.apply_eager_dataset_changes(ds).unlimited if r[:dataset_associations_join] case r[:type] when :many_to_many, :one_through_one ds = ds.join(r[:join_table], r[:right_keys].zip(r.right_primary_keys)) when :many_through_many, :one_through_many (r.reverse_edges + [r.final_reverse_edge]).each{|e| ds = ds.join(e[:table], e.fetch(:only_conditions, (Array(e[:left]).zip(Array(e[:right])) + Array(e[:conditions]))), :table_alias=>ds.unused_table_alias(e[:table]), :qualify=>:deep, &e[:block])} end end ds end