method
with_recursive
v8.0.0 -
Show latest stable
- Class:
ActiveRecord::QueryMethods
with_recursive(*args)public
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 posts_and_replies ON posts.in_reply_to_id = posts_and_replies.id) # ) # SELECT * FROM posts
See `#with` for more information.