Exist
滿酷的用法之前沒想過
```ruby
# Post
scope :famous, ->{ where("view_count > ?", 1_000) }
# User
scope :without_famous_post, ->{
where(_not_exists(Post.where("posts.user_id = users.id").famous))
}
def self._not_exists(scope)
"NOT #{_exists(scope)}"
end
def self._exists(scope)
"EXISTS(#{scope.to_sql})"
end
```
Subqueries
原來 rails 會自己 construct 這類的 sql
https://medium.com/@apneadiving/active-records-queries-tricks-2546181a98dd
沒有留言:
張貼留言