instance method with_recursive

Ruby on Rails 8.0.4

Since v7.2.3

Available in: v7.2.3 v8.0.4 v8.1.2

Signature

with_recursive(*args)

Add a recursive Common Table Expression (CTE) that you can then reference within another SELECT statement.

Post.with_recursive(post_and_replies: [Post.where(id: 42), Post.joins('JOIN post_and_replies ON posts.in_reply_to_id = post_and_replies.id')])
# => ActiveRecord::Relation
# WITH RECURSIVE post_and_replies AS (
#   (SELECT * FROM posts WHERE id = 42)
#   UNION ALL
#   (SELECT * FROM posts JOIN post_and_replies ON posts.in_reply_to_id = post_and_replies.id)
# )
# SELECT * FROM posts

See #with for more information.

Parameters

args rest
Source
# File activerecord/lib/active_record/relation/query_methods.rb, line 518
    def with_recursive(*args)
      check_if_method_has_arguments!(__callee__, args)
      spawn.with_recursive!(*args)
    end

Defined in activerecord/lib/active_record/relation/query_methods.rb line 518 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::QueryMethods

Type at least 2 characters to search.

↑↓ navigate · open · esc close