Fix Mongoid dependent strategy#258
Open
fibrasek wants to merge 2 commits intocharlotte-ruby:masterfrom
Open
Conversation
|
I know this is an old pull request, but it did fix some issues I was having on an application I work on. Hopefully my comment will help anyone googling for a solution. I was only able to find unrelated fixes for a different gem and it took a while to isolate the cause. The Problemclass FailingExample
include Mongoid::Document
is_impressionable counter_cache: true,
column_name: :impressions_field,
unique: :session_hash
field :impressions_field, default: 0, type: Integer
end
FailingExample.create!
FailingExample.last.destroy # Crash!
# => NameError (uninitialized constant Mongoid::Relations::Cascading::DeleteAll)The Solutionclass FixedExample
include Mongoid::Document
is_impressionable counter_cache: true,
column_name: :impressions_field,
unique: :session_hash
field :impressions_field, default: 0, type: Integer
# ADD THIS LINE TO FIX ERROR (overrides dependent: :delete_all):
has_many :impressions, as: :impressionable, dependent: :delete
end
FixedExample.create!
FixedExample.last.destroy # okNotesI am using Mongoid v6. |
Contributor
Author
|
Is this problem already solved? If not is it still relevant? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Again I ran againts some compability problems while using the gem with Mongoid. The first problem was the option
dependent: :delete_allbeing passed to the model, which is wrong for Mongoid as stated in the documentationThe other is not quite a problem, just a minor refactoring, since the relation is being setup in the
Impressionist::Impressionablemodule, there's no need to callImpressionist::SetupAssociation#setmethod.As far as I tested in my application (locally and in production) there's no bigger problems in those changes.
Note:
dependent: :deletedoes not triggers callbacks, I don't know if this can be a issue for some users.