mongoid-rspec icon indicating copy to clipboard operation
mongoid-rspec copied to clipboard

dependent: :destroy does not respect default_scope of related model

Open jnummelin opened this issue 8 years ago • 2 comments

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

jnummelin avatar Feb 07 '17 12:02 jnummelin

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

dblock avatar Jan 04 '18 19:01 dblock

@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. :)

jnummelin avatar Jan 04 '18 19:01 jnummelin