Skip to content

Remove specific relations from an ActiveRecord :joins or :includes

License

Notifications You must be signed in to change notification settings

gadogado/rescoped

Repository files navigation

Rescoped

Build Status

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:

  1. Remove any number of relations from :left_outer_joins.
Photo.left_outer_joins(:users, :likes, :region).remove_left_outer_joins(:users, :likes)
  1. Remove a single relation from the :joins
Photo.joins(:users, :likes).remove_joins(:users)
  1. The rescoped utilities can be used anywhere in the chain:
Photo.includes(:users, :likes).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])

About

Remove specific relations from an ActiveRecord :joins or :includes

Topics

Resources

License

Stars

Watchers

Forks

Packages