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

Fix Mongoid dependent strategy #258

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

fibrasek
Copy link
Contributor

Again I ran againts some compability problems while using the gem with Mongoid. The first problem was the option dependent: :delete_all being passed to the model, which is wrong for Mongoid as stated in the documentation
The other is not quite a problem, just a minor refactoring, since the relation is being setup in the Impressionist::Impressionable module, there's no need to call Impressionist::SetupAssociation#set method.

As far as I tested in my application (locally and in production) there's no bigger problems in those changes.

Note: dependent: :delete does not triggers callbacks, I don't know if this can be a issue for some users.

@RickCarlino
Copy link

RickCarlino commented Feb 28, 2019

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 Problem

class 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 Solution

class 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 # ok

Notes

I am using Mongoid v6.

@fibrasek
Copy link
Contributor Author

Is this problem already solved? If not is it still relevant?

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

Successfully merging this pull request may close these issues.

None yet

2 participants