Class: AdjustableSchema::ActiveRecord::QueryMethods::WithChain
- Inherits:
-
Object
- Object
- AdjustableSchema::ActiveRecord::QueryMethods::WithChain
- Defined in:
- lib/adjustable_schema/active_record/query_methods.rb
Instance Method Summary collapse
-
#initialize(scope) ⇒ WithChain
constructor
A new instance of WithChain.
-
#recursive(*args) ⇒ Object
Returns a new relation expressing WITH RECURSIVE statement.
Constructor Details
#initialize(scope) ⇒ WithChain
Returns a new instance of WithChain.
5 6 7 |
# File 'lib/adjustable_schema/active_record/query_methods.rb', line 5 def initialize scope @scope = scope end |
Instance Method Details
#recursive(*args) ⇒ Object
Returns a new relation expressing WITH RECURSIVE statement.
#recursive accepts conditions as a hash. See QueryMethods#with for more details on each format.
User.with.recursive(
descendants: User.joins('INNER JOIN descendants ON users.parent_id = descendants.id')
)
# WITH RECURSIVE descendants AS (
# SELECT * FROM users
# UNION
# SELECT * FROM users INNER JOIN descendants ON users.parent_id = descendants.id
# ) SELECT * FROM descendants
WARNING! Due to how Arel works,
* `recursive` can't be chained with any prior non-recursive calls to `with` and
* all subsequent non-recursive calls to `with` will be treated as recursive ones.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/adjustable_schema/active_record/query_methods.rb', line 26 def recursive *args args.map! do next _1 unless _1.is_a? Hash _1 .map(&method(:with_recursive_union)) .to_h end case @scope.with_values in [] @scope = @scope.with :recursive, *args in [ :recursive, * ] @scope = @scope.with *args else raise ArgumentError, "can't chain `WITH RECURSIVE` with non-recursive one" end @scope end |