lib/active_record/hierarchical_query/query.rb
# Specify root scope of the hierarchy.
#
# @example When scope given
#   MyModel.join_recursive do |hierarchy|
#     hierarchy.start_with(MyModel.where(parent_id: nil))
#              .connect_by(id: :parent_id)
#   end
#
# @example When Hash given
#   MyModel.join_recursive do |hierarchy|
#     hierarchy.start_with(parent_id: nil)
#              .connect_by(id: :parent_id)
#   end
#
# @example When String given
#    MyModel.join_recursive do |hierarchy|
#      hierararchy.start_with('parent_id = ?', 1)
#                 .connect_by(id: :parent_id)
#    end
#
# @example When block given
#   MyModel.join_recursive do |hierarchy|
#     hierarchy.start_with { |root| root.where(parent_id: nil) }
#              .connect_by(id: :parent_id)
#   end
#
# @example When block with arity=0 given
#   MyModel.join_recursive do |hierarchy|
#     hierarchy.start_with { where(parent_id: nil) }
#              .connect_by(id: :parent_id)
#   end
#
# @example Specify columns for root relation (PostgreSQL-specific)
#   MyModel.join_recursive do |hierarchy|
#     hierarchy.start_with { select('ARRAY[id] AS _path') }
#              .connect_by(id: :parent_id)
#              .select('_path || id', start_with: false) # `start_with: false` tells not to include this expression into START WITH clause
#   end
#
# @param [ActiveRecord::Relation, Hash, String, nil] scope root scope (optional).
# @return [ActiveRecord::HierarchicalQuery::Query] self
Specify root scope of the hierarchy.
Specify root scope of the hierarchy .
def start_with(scope = nil, *arguments, &block)
        raise ArgumentError, 'START WITH: scope or block expected, none given' unless scope || block
        case scope
          when Hash, String
            @start_with_value = klass.where(scope, *arguments)
          when ActiveRecord::Relation
            @start_with_value = scope
          else
        end
        if block
          object = @start_with_value || @klass
          @start_with_value = if block.arity == 0
            object.instance_eval(&block)
          else
            block.call(object)
          end
        end
        self
      end
def start_with ( scope = nil , * arguments , & block ) raise ArgumentError , 'START WITH: scope or block expected, none given' unless scope || block case scope when Hash , String @start_with_value = klass . where ( scope , * arguments ) when ActiveRecord :: Relation @start_with_value = scope else end if block object = @start_with_value || @klass @start_with_value = if block . arity == 0 object . instance_eval ( & block ) else block . call ( object ) end end self end
Specify root scope of the hierarchy.
Specify root scope of the hierarchy .
# do nothing if something weird given
None
ActiveRecord::HierarchicalQuery::Query
scope
<not_specific>
arguments
<not_specific>
block
<not_specific>
<not_specific>
scope root scope (optional).
scope root scope ( optional ) .
example
When scope given MyModel.join_recursive do |hierarchy| hierarchy.start_with(MyModel.where(parent_id: nil)) .connect_by(id: :parent_id) end
When scope given MyModel . join_recursive do |hierarchy| hierarchy . start_with ( MyModel . where ( parent_id : nil )) . connect_by ( id : : parent_id ) end
example
When Hash given MyModel.join_recursive do |hierarchy| hierarchy.start_with(parent_id: nil) .connect_by(id: :parent_id) end
When Hash given MyModel . join_recursive do |hierarchy| hierarchy . start_with ( parent_id : nil ) . connect_by ( id : : parent_id ) end
example
When String given MyModel.join_recursive do |hierarchy| hierararchy.start_with('parent_id = ?', 1) .connect_by(id: :parent_id) end
When String given MyModel . join_recursive do |hierarchy| hierararchy . start_with ( ' parent_id = ? ' 1 ) . connect_by ( id : : parent_id ) end
example
When block given MyModel.join_recursive do |hierarchy| hierarchy.start_with { |root| root.where(parent_id: nil) } .connect_by(id: :parent_id) end
When block given MyModel . join_recursive do |hierarchy| hierarchy . start_with { |root| root . where ( parent_id : nil ) } . connect_by ( id : : parent_id ) end
example
When block with arity=0 given MyModel.join_recursive do |hierarchy| hierarchy.start_with { where(parent_id: nil) } .connect_by(id: :parent_id) end
When block with arity = 0 given MyModel . join_recursive do |hierarchy| hierarchy . start_with { where ( parent_id : nil ) } . connect_by ( id : : parent_id ) end
example
Specify columns for root relation (PostgreSQL-specific) MyModel.join_recursive do |hierarchy| hierarchy.start_with { select('ARRAY[id] AS _path') } .connect_by(id: :parent_id) .select('_path || id', start_with: false) # `start_with: false` tells not to include this expression into START WITH clause end
Specify columns for root relation ( PostgreSQL - specific ) MyModel . join_recursive do |hierarchy| hierarchy . start_with { select ( ' ARRAY [ id ] AS _path ' ) } . connect_by ( id : : parent_id ) . select ( ' _path || id ' start_with : false ) # ` start_with : false ` tells not to include this expression into START WITH clause end