-
Notifications
You must be signed in to change notification settings - Fork 196
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
Recovery of dependent records fails due to validation error #166
Comments
You're saying 'Episode::Translation is not acting as a paranoid'. Do you mean the record gets fully deleted? Can you check whether the translation record exists in the database with You may need to recover the translation record first before recovering the main record. |
When it is deleted it is correctly marked as deleted by setting the deleted_at.
But when it is recovered only the episode is recovered
Update 1: |
When do you get the 'Globalized model must exist' error? |
Does this help:
|
No, I need to know what method leads to that error. From your first description I thought it happened when you call |
Here it is the test case
Update 1 |
I'm curious why you don't see the error in #166 (comment). Does the error happen with |
Yes, You are right. I don't see the error on .recover. Only on recover! (I don't know why). |
Ok, then I think I see what happens: ActsAsParanoid tries to recover your translations but their validation fails because they are recovered before the main object. When you just call In this case, it seems, the main record needs to be recovered first. I wonder if that should be made the default. |
Just to make the example more full - recover is also behaving differently for the rest of the associations. I have two more associations - content_to_content_pictures, and subscriptions accesses. By using .recover they get recovered like
But there is no query for episode_translations. At the end as there are no translations of the episode and I have a validation that it's title can't be blank, I get a "Title can't be blank" error on the episode as the translations are not recovered. |
This is how I managed to workaround it for the moment. Posting for reference if anybody is searching module TranslatableSoftdeletable
extend ActiveSupport::Concern
included do
translation_class.class_eval do
acts_as_paranoid
end
before_recover :restore_translations
end
def restore_translations
translations.with_deleted.each do |translation|
translation.deleted_at = nil
translation.save(validate: false)
end
self.translations.reload
end
end and the spec that is working it require 'rails_helper'
RSpec.describe "TranslatableSoftdeletable", type: :model do
it "destroys and recovers translations" do
instance = FactoryBot.create(:episode)
instance.destroy
expect(Episode.exists?(instance.id)).to eq false
expect(instance.translations.only_deleted.count).to eq 1
instance.recover
expect(instance.errors.to_a).to eq []
instance.reload
expect(Episode.exists?(instance.id)).to eq true
expect(instance.translations.count).to eq 1
end
end |
@thebravoman , thanks for you workaround. |
This is probably fixed by #227. |
Using Globalized
I have
This translated from globalized to
So we have
I've added a deleted_at column on Episode::Translations. I've filled it with
But when I try to recover an error is thrown "Globalized model must exist". This error is because Episode::Translation expects the record it is attached to it. But it is not because it is not recovered yet and somehow the translation class - Episode::Translation is not acting as a paranoid.
The text was updated successfully, but these errors were encountered: