Module: ActiveRecordExtended::LtreeQueryMethods
- Defined in:
- lib/active_record_extended/ltree_query_methods.rb
Overview
Mixed into ActiveRecord::QueryMethods to add where.ltree_contains,
where.ltree_depth, and the related ltree-aware select / order helpers
described in the file header.
Instance Method Summary collapse
-
#ltree_contains(column, ids, array: true) ⇒ ActiveRecord::Relation
ltree contains check - does the path contain a specific subpath anywhere? Uses the index() function which has no simple Arel equivalent.
-
#ltree_depth(column, depth, operator = :eq) ⇒ ActiveRecord::Relation
ltree depth check using nlevel().
Instance Method Details
#ltree_contains(column, ids, array: true) ⇒ ActiveRecord::Relation
ltree contains check - does the path contain a specific subpath anywhere?
Uses the index() function which has no simple Arel equivalent.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/active_record_extended/ltree_query_methods.rb', line 88 def ltree_contains(column, ids, array: true) ids = [ids].flatten.compact.filter_map { |id| Integer(id, exception: false) } return none_query if ids.empty? col = column.to_s.include?('.') ? column.to_s : "#{table_name}.#{column}" if array @scope.where( "EXISTS (SELECT 1 FROM unnest(#{col}) AS p, unnest(ARRAY[?]::ltree[]) AS id WHERE index(p, id) >= 0)", ids.map(&:to_s) ) else @scope.where( "EXISTS (SELECT 1 FROM unnest(ARRAY[?]::ltree[]) AS id WHERE index(#{col}, id) >= 0)", ids.map(&:to_s) ) end end |
#ltree_depth(column, depth, operator = :eq) ⇒ ActiveRecord::Relation
ltree depth check using nlevel()
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/active_record_extended/ltree_query_methods.rb', line 118 def ltree_depth(column, depth, operator = :eq) op = case operator when :eq then '=' when :gt then '>' when :gte then '>=' when :lt then '<' when :lte then '<=' else raise ArgumentError, "Invalid operator: #{operator}. Use :eq, :gt, :gte, :lt, :lte" end @scope.where("nlevel(#{table_name}.#{column}) #{op} ?", depth) end |