How to disable default_scope for a belongs_to?

Is there a way to disable the default_scope for a single belongs_to association? The default_scope is fine for all but a single belongs_to that I would like to bypass the scope. I'm familiar with with_exclusive_scope however I don't think that can be used with belongs_to.

Any suggestions?

Context: I'm trying to allow the branch_source association in acts_as_revisable to point to a revision that is not the latest (revisable_is_current is false).


Probably a bit late to the party (just short of 3 years), but just run into the same problem and the solution from Tobias is certainly the right direction, but it can be simplified for Rails 3.2+. The only thing I still don't like is the "hard coded" class name for Document, maybe it's possible to inflect using reflection...

Anyhow this is what I've come up with:

class Comment < ActiveRecord::Base
  # Document has some kind of default_scope
  belongs_to :document

  # Ensure document is not scoped, because Rails 3.2 uses modules it's
  # possible to use simple inheritance.
  def document
    Document.unscoped { super }

Update: got a generic solution, based on reflect_on_association

belongs_to :account, -> { unscope(where: :destroyed_at) }

works for me, Rails 4.1

Just had this problem myself, and here's what I came up with:

class Comment < ActiveRecord::Base
  belongs_to :document # Document has some kind of default scope
                       # that stops us from finding it

  # Override getter method for document association
  def document_with_unscoped
    # Fetch document with default scope disabled
    Document.unscoped { document_without_unscoped }
  alias_method_chain :document, :unscoped

I removed this

belongs_to :document

and replaced it with

def document
    Document.unscope(where: :deleted_at).find_by(id: document_id)

def document=(d)
    self.document_id = d&.id

Need Your Help

WPF stretch control inside a UniformGrid inside a Grid

c# wpf

I'm having a bit of a hassle making my controls stretch in WPF so hopefully someone will know the answer. Basically I have a 4 row grid, with rows defined as follows: