How To Remove A Default Scope For An Association In Rails

Posted by Weston Ganger

I needed to disable a default scope for another model in my belongs_to because I am using acts_as_paranoid.

In Rails 4.1+ they provide a way to do this but it isn’t that reliable. belongs_to :user, -> {unscope(where: :deleted_at)}. It has problems with SQL strings though.

Here’s how to do it in Rails 3.2+, which is a much better solution if you ask me.


class Post < ActiveRecord::Base
  belongs_to :user

  def user
    User.unscoped { super }
  end
end


Related External Links:

Article Topic:Software Development - Rails

Date:April 03, 2016

Recommended Posts