How to handle associations with Discard
masterUnlike paranoia, Discard does not automatically destroy dependent associations. This prevents accidental data loss. Instead, you should manage associations using one of two patterns:
1. Independent Discarding
Keep records independent. For example, a Comment can remain 'kept' even if its parent Post is discarded. You simply query for kept records using the kept scope.
2. Dependent Scoping
If a child record should only be considered 'kept' if its parent is also 'kept', override the child's kept scope using a join and a merge.
class Comment < ActiveRecord::Base
belongs_to :post
include Discard::Model
# Only returns comments where both the comment and the post are kept
scope :kept, -> { undiscarded.joins(:post).merge(Post.kept) }
def kept?
undiscarded? && post.kept?
end
endclass Comment < ActiveRecord::Base
belongs_to :post
include Discard::Model
scope :kept, -> { undiscarded.joins(:post).merge(Post.kept) }
def kept?
undiscarded? && post.kept?
end
end