class Sequel::Plugins::ManyThroughMany::ManyThroughManyAssociationReflection
The AssociationReflection subclass for many_through_many associations.
Constants
- FINALIZE_SETTINGS
Public Instance Methods
cloneable?(ref)
click to toggle source
many_through_many and one_through_many associations can be clones
# File lib/sequel/plugins/many_through_many.rb 87 def cloneable?(ref) 88 ref[:type] == :many_through_many || ref[:type] == :one_through_many 89 end
default_associated_key_alias()
click to toggle source
The default associated key alias(es) to use when eager loading associations via eager.
# File lib/sequel/plugins/many_through_many.rb 93 def default_associated_key_alias 94 self[:uses_left_composite_keys] ? (0...self[:through].first[:left].length).map{|i| :"x_foreign_key_#{i}_x"} : :x_foreign_key_x 95 end
finalize_settings()
click to toggle source
# File lib/sequel/plugins/many_through_many.rb 112 def finalize_settings 113 FINALIZE_SETTINGS 114 end
join_table_alias()
click to toggle source
The alias for the first join table.
# File lib/sequel/plugins/many_through_many.rb 117 def join_table_alias 118 final_reverse_edge[:alias] 119 end
reciprocal()
click to toggle source
Many through many associations don't have a reciprocal
# File lib/sequel/plugins/many_through_many.rb 122 def reciprocal 123 nil 124 end
Private Instance Methods
_associated_dataset()
click to toggle source
# File lib/sequel/plugins/many_through_many.rb 128 def _associated_dataset 129 ds = associated_class 130 (reverse_edges + [final_reverse_edge]).each do |t| 131 h = {:qualify=>:deep} 132 if t[:alias] != t[:table] 133 h[:table_alias] = t[:alias] 134 end 135 ds = ds.join(t[:table], Array(t[:left]).zip(Array(t[:right])), h) 136 end 137 ds 138 end
calculate_edges()
click to toggle source
Transform the :through option into a list of edges and reverse edges to use to join tables when loading the association.
# File lib/sequel/plugins/many_through_many.rb 158 def calculate_edges 159 es = [{:left_table=>self[:model].table_name, :left_key=>self[:left_primary_key_column]}] 160 self[:through].each do |t| 161 es.last.merge!(:right_key=>t[:left], :right_table=>t[:table], :join_type=>t[:join_type]||self[:graph_join_type], :conditions=>(t[:conditions]||[]).to_a, :block=>t[:block]) 162 es.last[:only_conditions] = t[:only_conditions] if t.include?(:only_conditions) 163 es << {:left_table=>t[:table], :left_key=>t[:right]} 164 end 165 es.last.merge!(:right_key=>right_primary_key, :right_table=>associated_class.table_name) 166 edges = es.map do |e| 167 h = {:table=>e[:right_table], :left=>e[:left_key], :right=>e[:right_key], :conditions=>e[:conditions], :join_type=>e[:join_type], :block=>e[:block]} 168 h[:only_conditions] = e[:only_conditions] if e.include?(:only_conditions) 169 h 170 end 171 reverse_edges = es.reverse.map{|e| {:table=>e[:left_table], :left=>e[:left_key], :right=>e[:right_key]}} 172 reverse_edges.pop 173 calculate_reverse_edge_aliases(reverse_edges) 174 final_reverse_edge = reverse_edges.pop 175 final_reverse_alias = final_reverse_edge[:alias] 176 177 h = {:final_edge=>edges.pop, 178 :final_reverse_edge=>final_reverse_edge, 179 :edges=>edges, 180 :reverse_edges=>reverse_edges, 181 :predicate_key=>qualify(final_reverse_alias, edges.first[:right]), 182 :associated_key_table=>final_reverse_edge[:alias], 183 } 184 h.each{|k, v| cached_set(k, v)} 185 h 186 end
calculate_reverse_edge_aliases(reverse_edges)
click to toggle source
Make sure to use unique table aliases when lazy loading or eager loading
# File lib/sequel/plugins/many_through_many.rb 141 def calculate_reverse_edge_aliases(reverse_edges) 142 aliases = [associated_class.table_name] 143 reverse_edges.each do |e| 144 table_alias = e[:table] 145 if aliases.include?(table_alias) 146 i = 0 147 table_alias = while true 148 ta = :"#{table_alias}_#{i}" 149 break ta unless aliases.include?(ta) 150 i += 1 151 end 152 end 153 aliases.push(e[:alias] = table_alias) 154 end 155 end
filter_by_associations_limit_key()
click to toggle source
# File lib/sequel/plugins/many_through_many.rb 188 def filter_by_associations_limit_key 189 fe = edges.first 190 Array(qualify(fe[:table], fe[:right])) + Array(qualify(associated_class.table_name, associated_class.primary_key)) 191 end