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

Fixed bug not to restore association records #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kouyaf77
Copy link

Actually, we couldn't get association records at relation = self.send(name) of lib/mongoid/paranoia.rb.
In Rspec, subject had reference. But instance doesn't have reference when we use excepting Rspec.
So I fixed it.
Thx

@simi
Copy link
Owner

simi commented Feb 22, 2015

Hello @kouyaf77. Thanks for your pull request. Can you provide a little info why this change is needed? What's the usecase? Why you couldn't get association records via relation = self.send(name)?

@kouyaf77
Copy link
Author

Hi @simi
There are some models the following.

class User
  include Mongoid::Document
  include Mongoid::Paranoia

  has_many :posts, dependent: :destroy
end

class Post
  include Mongoid::Document
  include Mongoid::Paranoia

  belongs_to :user
end

Then, we can't restore association records in case of following.
Because relation = self.send(name) is not got posts.

User.create posts: Post.create
user = User.first
user.destroy

user = User.unscoped.first
user.restore(recursive: true)

@kouyaf77
Copy link
Author

The reason not to get association records is to work default scope.
So I fixed not to work default scope.

@johnnyshields
Copy link
Collaborator

@kouyaf77 Thank you for the PR and for fixing the tests. I'm a bit concerned about the change to Mongoid::Relations::Synchronization. This looks like it will leave in inverse references to paranoid documents which have been destroyed. I think it's better to remove the inverse references on paranoid destroy then recreate them after paranoid restore.

@johnnyshields
Copy link
Collaborator

Just looking at this again. Another option would be to move the deleted key to a shadow field (e.g. _deleted_user_id) and then restore the value later.

Again the main reason I don't like the solution in the PR is that it leaves you with a relation reference to a deleted object, which is a "messy" data state that doesn't (shouldn't) occur naturally in Mongoid.

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.

3 participants