Skip to content

Instantly share code, notes, and snippets.

@lunalium
Forked from lwe/unscoped.rb
Created February 2, 2016 04:36
Show Gist options
  • Save lunalium/16bfcf3da78acc3f12f5 to your computer and use it in GitHub Desktop.
Save lunalium/16bfcf3da78acc3f12f5 to your computer and use it in GitHub Desktop.

Revisions

  1. @lwe lwe created this gist Jun 13, 2012.
    46 changes: 46 additions & 0 deletions unscoped.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    # Provides the ability to unscope associations, this solves problems described in
    # http://stackoverflow.com/questions/1540645/how-to-disable-default-scope-for-a-belongs-to/11012633#11012633
    #
    # Examples
    #
    # class Document < ActiveRecord::Base
    # default_scope where(deleted: false)
    # end
    #
    # class Comment < ActiveRecord::Base
    # extend Unscoped
    #
    # belongs_to :document
    # unscope :document
    # end
    #
    module Unscoped

    # Public: Ensure a previously defined association is not scoped by a default_scope.
    #
    # associations - The Symbol with the name(s) of the associations to unscope.
    #
    # Examples
    #
    # # Unscope a single assoication
    # unscope :document
    #
    # # Unscope multiple in one way.
    # unscope :document, :comments
    #
    # Raises ArgumentError if the named association does not exist on the model, so ensure
    # the association is defined _before_ calling `unscope`.
    #
    # Returns nothing.
    def unscope(*associations)
    associations.flatten.each do |association|
    raise ArgumentError, "no association named #{association} exists on this model" unless self.reflect_on_association(*association)

    class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{association}
    self.class.reflect_on_association(#{association.inspect}).klass.unscoped { super }
    end
    RUBY
    end
    end
    end