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 does not respect default_scope of related model #182

Open
jnummelin opened this issue Feb 7, 2017 · 2 comments
Open
Labels

Comments

@jnummelin
Copy link

I have a has_many relation defined with dependent: :destroy like so:

class HostNode
  include Mongoid::Document
  include Mongoid::Timestamps

  has_many :containers, dependent: :destroy

On the other side of the relation, there's a default_scope defined:

class Container
  include Mongoid::Document
  include Mongoid::Timestamps

  field :deleted_at, type: Time
  field :container_type, type: String, default: 'container'
  default_scope -> { where(deleted_at: nil, container_type: 'container') }

Now within specs when I delete a HostNode model all the Containers are deleted, even those that do NOT match the default_scope. If I do the same operation WITHOUT mongoid-rspec loaded only the containers matching the default_scope gets deleted as expected.

This is somewhat un-expected in my opinion. Why does it behave differently when mongoid-rspec is loaded and is there any way to control this? Tried to look from the code but failed miserably. :D

@dblock
Copy link
Collaborator

dblock commented Jan 4, 2018

This is old, but would appreciate a spec/PR with maybe even a fix @jnummelin!

@jnummelin
Copy link
Author

@dblock I'll see if I can wrap my head around the code base to figure it out. Given that I find some time to actually do it. :)

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

No branches or pull requests

2 participants