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

really_destroy! on deleted record with a has_one association #414

Open
studious opened this issue Aug 1, 2017 · 5 comments
Open

really_destroy! on deleted record with a has_one association #414

studious opened this issue Aug 1, 2017 · 5 comments

Comments

@studious
Copy link

studious commented Aug 1, 2017

I'm trying to call really_destroy! on a record that has already been destroyed. The trouble is that it has a 'has_one' association and when that has_one is not deleted the parent record can't either due to a foreign key constraint.

I can see recursive restore handles 'has_one' association but really_destroy doesn't appear to. There is a comment about has_one returning nil and at this point

What's the best way to achieve this?

@harryhlai
Copy link

harryhlai commented Nov 2, 2017

We just ran into this same issue today. What I'm currently checking is to see if all our models that are on the other end of those has_ones can be switched to use without_default_scope: true. That would fix this issue, but it means anything else referencing those models will not filter out soft-deleted records by default. If anyone has found a better workaround, I'd appreciate hearing about it. =)

@harryhlai
Copy link

I ended up submitting a PR to fix this.

#428

@joergschiller
Copy link
Contributor

Until your PR is merged I stick to this workaround:

Instead of

has_one :foo, dependent: :destroy

Use

has_one :foo
has_one :foo_with_deleted, -> { with_deleted }, dependent: :destroy, class_name: 'Foo'

@LeipeLeon
Copy link

An other option would be adding a foo_only_deleted scope:

has_one :foo, dependent: :destroy
has_one :foo_only_deleted, -> { only_deleted }, dependent: :destroy, class_name: "Foo"

@mathieujobin
Copy link
Collaborator

anybody here think PR #428 still worth it? or I should close ?

the workaround by @joergschiller and @LeipeLeon looks sufficiant

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

No branches or pull requests

5 participants