How to disable default_scope for a belongs_to?

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 }
  end
end

Update: got a generic solution, based on reflect_on_association https://gist.github.com/2923336

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)