Last active
December 17, 2015 11:19
-
-
Save delba/5601109 to your computer and use it in GitHub Desktop.
Unscoping a model in an association
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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