Proposal:
Introduce an alias_scope
class method in ActiveRecord::Scoping::Named::ClassMethods
that allows one scope to act as an alias for another.
Example Usage:
class Product < ApplicationRecord
scope :active, -> { where(active: true) }
alias_scope :enabled, :live, :published, :active
end
Product.active # => returns all active products
Product.enabled # => same as above
Product.live # => same as above
Product.published # => same as above
Benefits:
- Reduces duplication for commonly aliased scopes.
- Improves readability and maintainability of model code.
- Keeps the API consistent with Rails’ existing
scope
DSL.
Implementation Notes:
- Minimal change: just a helper method that delegates to an existing scope.
- Fully backward-compatible.
- Automated tests can verify that the alias behaves identically to the original scope.
Discussion Points:
- Any concerns about adding this to ActiveRecord’s public API?
I’d love to hear feedback on whether this aligns with Rails conventions and if maintainers would be open to adding it if I opened a PR?