Module: ActiveRecord::HierarchicalQuery

Defined in:
lib/active_record/hierarchical_query.rb,
lib/active_record/hierarchical_query/query.rb,
lib/active_record/hierarchical_query/version.rb,
lib/active_record/hierarchical_query/orderings.rb,
lib/active_record/hierarchical_query/cte/columns.rb,
lib/active_record/hierarchical_query/join_builder.rb,
lib/active_record/hierarchical_query/cte/union_term.rb,
lib/active_record/hierarchical_query/cte/query_builder.rb,
lib/active_record/hierarchical_query/cte/cycle_detector.rb,
lib/active_record/hierarchical_query/cte/recursive_term.rb,
lib/active_record/hierarchical_query/cte/non_recursive_term.rb

Defined Under Namespace

Modules: CTE Classes: JoinBuilder, Orderings, Query

Constant Summary collapse

DELEGATOR_SCOPE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

ActiveRecord::VERSION::STRING < '4.0.0' ? :scoped : :all
VERSION =
'1.4.5'

Instance Method Summary collapse

Instance Method Details

#join_recursive(join_options = {}) {|query| ... } ⇒ Object

Performs a join to recursive subquery which should be built within a block.

Examples:

MyModel.join_recursive do |query|
  query.start_with(parent_id: nil)
       .connect_by(id: :parent_id)
       .where('depth < ?', 5)
       .order_siblings(name: :desc)
end

Parameters:

  • join_options (Hash) (defaults to: {})

Options Hash (join_options):

  • :as (String, Symbol)

    aliased name of joined table (‘%table_name%__recursive` by default)

Yields:

  • (query)

Yield Parameters:

Raises:

  • (ArgumentError)

    if block is omitted



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/active_record/hierarchical_query.rb', line 31

def join_recursive(join_options = {}, &block)
  raise ArgumentError, 'block expected' unless block_given?

  query = Query.new(klass)

  if block.arity == 0
    query.instance_eval(&block)
  else
    block.call(query)
  end

  query.join_to(self, join_options)
end