Mongoid removes a subset of (invalid) inline documents in one go

This question Mondoid deleting inline documents is not saved explains how to delete either all subdocuments or one at a time

I am looking for a way to remove all my invalid inline documents in one go

model User
  embeds_many :posts
end
model Post
  embedded_in :user
end

      

Suppose I have a user with multiple invalid posts, how can I remove them from db? The following do not work (most likely for a reason similar to the one in the linked question)

user.posts.reject(&:valid).each(&:destroy)

      

I am looking for something like

user.post.reject(&:valid?).each(:mark_as_destruction)
user.save

      

But marked_for_destruction

only exists in the c version of the request ?

, not as an actual installer.

Any idea? How do you usually clean up invalid nested records ?

Note. My typical case of invalid occurrence is that my inline document is referencing an external document (with validation presence_of

) and that the external document is being deleted without properly executing callbacks

0


source to share





All Articles