Module: LowCardTables::ActiveRecord::Scoping

Extended by:
ActiveSupport::Concern
Included in:
ActiveRecord::Base
Defined in:
lib/low_card_tables/active_record/scoping.rb

Overview

This module gets included into ::ActiveRecord::Scoping. It overrides #scope to do one thing, and one thing only: it checks to see if you’re defining a scope that constrains on low-card columns, statically. (‘Statically’ here means the deprecated-in-Rails-4.0+ style of ‘scope :foo, where(…)’ rather than ‘scope :foo { where(…) }’.) If it finds that you’re trying to define such a scope, it throws an error.

Why does it do this? Statically-defined scopes have their #where calls evaluated only once, at class-load time. But part of the design of the low-card system is that new low-card rows can be added at runtime, and new rows may well fit whatever constraint you’re applying in this scope. The low-card system translates calls such as this:

where(:deleted => false)

into clauses like this:

WHERE user_status_id IN (1, 3, 4, 5, 8, 9, 12)

Because new rows can be added at any time, the list of status IDs needs to be able to be computed dynamically. Static scopes prevent this, so we detect this condition here and raise an exception when it occurs.

This sort of problem, by the way, is one of the reasons why static scope definitions are deprecated in Rails 4.x.

Defined Under Namespace

Modules: ClassMethods