rails/rails — pull request #17016 by bogdan (2014-09-22 22:50:02+00:00)
Title: Define a scope that is NOT version of another scope

I am having troubles reversing a scope conditions and supporting a mirror relations where one relation is a `not` version of another.

So Propose the following API to manage such case:

``` ruby
class Post < ActiveRecord::Base

scope :active, -> { where(active: true) }
reverse_scope :inactive, :active

scope :created_before, ->(date) { where("posts.created_at < date")
reverse_scope :created_after, :created_before

scope :trending, -> { joins(:category).where(categories: {treding: true})
reverse_scope :abandon, :trending

end

Post.inactive # => WHERE `active` != 1
Post.created_after('1986-08-05') # => WHERE NOT(posts.created_at < '1986-08-05')
Post.abandon # => INNER JOIN `categories` ON `categories`.id = `posts`.`category_id`
# WHERE `categories`.`treding` != 1

```

Here is bonus method `reverse_where` that is used under the hood:

``` ruby
Post.active.reverse_where
```

I've make it public and documented because people might want to use it directly.
But `reverse_scope` is a primary use case that is convenient.