ActsAsParanoid / acts_as_paranoid

ActiveRecord plugin allowing you to hide and restore records without actually deleting them.
MIT License
1.45k stars 190 forks source link

Added `with_deleted` option to `has_one` relationship #324

Open marcomd opened 4 months ago

marcomd commented 4 months ago

With this PR it is possible to create a has_one relationship of deleted records. I added two new tests and I also updated the README

class Parent < ActiveRecord::Base
  has_one :child, class_name: "ParanoiacChild"
  has_one :child_with_deleted, class_name: "ParanoiacChild", with_deleted: true
end

class ParanoiacChild < ActiveRecord::Base
  acts_as_paranoid
  belongs_to :parent
end

parent = Parent.first

child = ParanoiacChild.create
parent.child = child

parent.child #=> ParanoiacChild

child.destroy
parent.reload

parent.child #=> nil
parent.child_with_deleted #=> ParanoiacChild
marcomdiubenda commented 4 months ago

It's like

has_one :child_with_deleted, -> { with_deleted }, class_name: "ParanoiacChild"

It came to mind while I was writing the description . Up to you whether to keep this PR or discard it. However, I find useful the updated README with an example with has_one