Rescoped
Rescoped allows you to remove specific relations from an activerecord :joins or :includes with some new utilities, remove_includes, remove_joins, and remove_left_outer_joins
Usage
Consider the following scope:
class Photo < ApplicationRecord
scope :public_api, lambda {
includes(
:users,
:likes,
:region
).viewable.recent
}
end
Rescoped let's you keep an existing scope but without the same relations structure. Here's an example of keeping the :public_api scope but removing one of the relations from the :includes
# Avoid an includes(:relation).joins(:relation) and instead restructure as a simple INNER JOIN
Photo.public_api.remove_includes(:region).joins(:region)
Some other simple examples:
- Remove any number of relations from
:left_outer_joins.
Photo.left_outer_joins(:users, :likes, :region).remove_left_outer_joins(:users, :likes)
Remove a single relations from the
:joinsPhoto.joins(:users, :likes).remove_joins(:users)The rescoped utilities can be used anywhere in the chain:
Photo.includes(:users).left_joins(:regions).viewable.recent.remove_includes(:users)
Installation
gem 'rescoped'
License
The gem is available as open source under the terms of the MIT License.
Author
Geoff Ereth ([email protected])