Clear ORM
Search…
Joins
In Clear, since every Collection is a SelectBuilder object, you can write very complex queries in a simple fashion. Let's see this example:
1
# Get the posts of the users which have more than X posts:
2
def user_with_more_than_x_posts(post_count)
3
User.query
4
.select("users.id as id")
5
.inner_joins("posts"){ posts.user_id == users.id }
6
.group_by("users.id")
7
.having{ raw("COUNT(*)") > post_count }
8
end
9
10
# Get the posts of the users with more than 10 posts:
11
Post.query.where{ user_id.in?(user_with_more_than_x_posts(10)) }
Copied!

Joins

Joins are built using inner_join, left_join, right_join, cross_join or simply join method. An optional block is requested for condition:
1
# Retrieve users with supervisors
2
User.query.left_joins("users as u2"){ users.supervisor_id == u2.id }
Copied!
Additionally, optional parameter lateral can be set to true to create a LATERAL JOIN.
Last modified 2yr ago
Copy link
Contents
Joins