Rails

How To Remove A Default Scope For An Association In Rails

Posted by Weston Ganger on April 03, 2016

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:


Posted in Rails and Tagged with rails active-record 


Want me to help develop your next project or application?

Contact Us

Specializing in Ruby-on-Rails, Javascript, Multi-Platform Electron Desktop Apps and Hybrid Cordova Mobile Apps.

Recommended Posts