instance method
with_recursive
Ruby on Rails 7.2.3
Since v7.2.3Signature
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 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
-
argsrest
Source
# File activerecord/lib/active_record/relation/query_methods.rb, line 511
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 511
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::QueryMethods