Module Sequel::Plugins::DatasetAssociations::DatasetMethods
In: lib/sequel/plugins/dataset_associations.rb

Methods

Public Instance methods

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.

[Source]

     # File lib/sequel/plugins/dataset_associations.rb, line 80
 80:         def associated(name)
 81:           raise Error, "unrecognized association name: #{name.inspect}" unless r = model.association_reflection(name)
 82:           ds = r.associated_class.dataset
 83:           sds = opts[:limit] ? self : unordered
 84:           ds = case r[:type]
 85:           when :many_to_one
 86:             ds.filter(r.qualified_primary_key=>sds.select(*Array(r[:qualified_key])))
 87:           when :one_to_one, :one_to_many
 88:             r.send(:apply_filter_by_associations_limit_strategy, ds.filter(r.qualified_key=>sds.select(*Array(r.qualified_primary_key))))
 89:           when :many_to_many, :one_through_one
 90:             mds = r.associated_class.dataset.
 91:               join(r[:join_table], r[:right_keys].zip(r.right_primary_keys)).
 92:               select(*Array(r.qualified_right_key)).
 93:               where(r.qualify(r.join_table_alias, r[:left_keys])=>sds.select(*r.qualify(model.table_name, r[:left_primary_key_columns])))
 94:             ds.filter(r.qualified_right_primary_key=>r.send(:apply_filter_by_associations_limit_strategy, mds))
 95:           when :many_through_many, :one_through_many
 96:             fre = r.reverse_edges.first
 97:             fe, *edges = r.edges
 98:             edges << r.final_edge
 99:             mds = model.
100:               select(*Array(r.qualify(fre[:table], fre[:left]))).
101:               join(fe[:table], Array(fe[:right]).zip(Array(fe[:left])), :implicit_qualifier=>model.table_name).
102:               where(r.qualify(fe[:table], fe[:right])=>sds.select(*r.qualify(model.table_name, r[:left_primary_key_columns])))
103:             edges.each{|e| mds = mds.join(e[:table], Array(e[:right]).zip(Array(e[:left])))}
104:             ds.filter(r.qualified_right_primary_key=>r.send(:apply_filter_by_associations_limit_strategy, mds))
105:           when :pg_array_to_many
106:             ds.filter(Sequel[r.primary_key=>sds.select{Sequel.pg_array_op(r.qualify(r[:model].table_name, r[:key])).unnest}])
107:           when :many_to_pg_array
108:             ds.filter(Sequel.function(:coalesce, Sequel.pg_array_op(r[:key]).overlaps(sds.select{array_agg(r.qualify(r[:model].table_name, r.primary_key))}), false))
109:           else
110:             raise Error, "unrecognized association type for association #{name.inspect}: #{r[:type].inspect}"
111:           end
112: 
113:           ds = r.apply_eager_dataset_changes(ds).unlimited
114: 
115:           if r[:dataset_associations_join]
116:             case r[:type]
117:             when :many_to_many, :one_through_one
118:               ds = ds.join(r[:join_table], r[:right_keys].zip(r.right_primary_keys))
119:             when :many_through_many, :one_through_many
120:               (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])}
121:             end
122:           end
123: 
124:           ds
125:         end

[Validate]