rails/arel — pull request #210 by shoe (2013-10-09 02:31:03+00:00)
Title: Add a visitor to enable checking if a relation #would_include? an object...

....

This provides a mechanism to check, without actually executing a
query, whether a given object matches the conditions represented by
the WHERE clause of a relation.

This is useful in several cases.

Example 1: When you can avoid re-executing a query by knowing whether
or not a record you just inserted would be returned if the query were
to be re-executed. (e.g. Cache invalidation)

Example 2: When you are storing or manipulating conditions as
first-class entities, as done by https://github.com/ernie/ransack and
want know which of many queries would return a given object, without
executing each query.

Example 3: When you're already representing a complex condition as an
arel relation, and want to avoid duplication of code that would
re-implement the same condition in ruby as a predicate instance
method. e.g. ActiveRecord example:

scope :active, -> { where(state: 'active')}

def active?
state == 'active'
end

You would now be able to write: User.active.would_include?(user)