method

with_recursive

rails latest stable - Class: ActiveRecord::QueryMethods

Method not available on this version

This method is only available on newer versions. The first available version (v7.2.3) is shown here.

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 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.