Skip to content

Instantly share code, notes, and snippets.

@delba
Last active December 17, 2015 11:19
Show Gist options
  • Select an option

  • Save delba/5601109 to your computer and use it in GitHub Desktop.

Select an option

Save delba/5601109 to your computer and use it in GitHub Desktop.
Unscoping a model in an association
class Post < ActiveRecord::Base
has_many :comments
default_scope { where(public: true) }
end
class Comment < ActiveRecord::Base
belongs_to :post
default_scope { where(deleted_at: nil) }
def self.with_deleted
all.tap { |rel| rel.default_scoped = false }
end
end
Post.first.comments.with_deleted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment