Method: ThroughHierarchy::Hierarchicals::Hierarchical#join_best_rank
- Defined in:
- lib/through_hierarchy/hierarchicals/hierarchical.rb
#join_best_rank(group_by: nil) ⇒ Object
Join @model to @source only on best hierarchy matches FASTER METHOD: join source to source alias on source.rank < alias.rank where alias does not exist This performs OK. TODO: return arel once we know how to use the binds properly
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/through_hierarchy/hierarchicals/hierarchical.rb', line 97 def join_best_rank(group_by: nil) better_rank = spawn(@source.alias("better_hierarchy")) join_condition_array = [ better_rank.filters, better_rank.hierarchy_rank.lt(hierarchy_rank) ] join_condition_array << better_rank.source[group_by].eq(@source[group_by]) if group_by.present? arel = @model.arel_table. join(@source).on(filters). join(better_rank.source, Arel::Nodes::OuterJoin). on(and_conditions(join_condition_array)). where(better_rank.source[:id].eq(nil)) result = @model.joins(@hierarchy).joins(arel.join_sources).order(arel.orders) arel.constraints.each{|cc| result = result.where(cc)} return result end |