Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dependent: :destroy for associated models #12

Open
rdetert opened this issue Jan 1, 2014 · 3 comments
Open

dependent: :destroy for associated models #12

rdetert opened this issue Jan 1, 2014 · 3 comments

Comments

@rdetert
Copy link

rdetert commented Jan 1, 2014

I have a Document model associated with a Company. When I call @company.destroy, the Company record is lazy-deleted but the Documents are actually deleted.

class Company
  include Mongoid::Document
  include Mongoid::Paranoia
  has_many :documents, dependent: :destroy
end
class Document
  include Mongoid::Document
  include Mongoid::Paranoia
  belongs_to :company
end
@simi
Copy link
Owner

simi commented Jan 6, 2014

Thanks for suggestion @rdetert. It will be nice if you can add some failing test. Meanwhile I'll do research how another similar libraries are handling this.

@johnnyshields
Copy link
Collaborator

@rdetert I agree this needs to be looked into. I've also found a few cases where I think destroying paranoid docs with other paranoid docs relations can cause infinite recursion.

I've made some test models here which should help in writing specs:
https://github.com/simi/mongoid_paranoia/blob/master/spec/app/models/relations.rb

@abrisse
Copy link

abrisse commented Sep 2, 2015

has_many :documents, dependent: :destroy should work fine since all the models are instantiated first and then the #destroy is applied on each of them, so the entries will still be persisted in mongodb with a field deleted_at.

Nevertheless beware when using has_many :documents, dependent: :delete since the documents would in that case really be deleted since the delete_all is used in that case, which is not compliant with mongoid_paranoia

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants