ruby-on-railsruby-paranoia

Rails 5 Paranoia - How can I 'show' a deleted record?


Upon soft-deleting a record, I'm unable to call it on the show action on the controller, for it looks for a record matching the record's ID WHERE deleted_at IS NULLL, which is correct given the purpose of the gem, but I'd still like to be able to access it on a sort of a "readonly" state, within the application, in order to allow the user to review the archive and possibly restore it.

How can I work around the deletion scope so that I can access the object again?

UPDATE 1

By @slowjack2k's advice, I can access the soft-deleted records with the following query:

 @area = Area.only_deleted.find(params[:id])

A new problem arose afterwards, due to CanCanCan's load_and_authorize_resource: it attempts to call

 @area = Area.find(params[:id])

ignoring the only_deleted filter, resulting in error since the selected id is only found where deleted_at is not null (not deleted), and disabling the authorization "fixes" it, so it must be an issue between CanCanCan and Paranoia.

Here's a thread with the exact same issue: https://github.com/rubysherpas/paranoia/issues/356

Here's the new issue thread on StackOverflow: Rails 5 compatibility between Paranoia and CanCanCan, compromised?

I'll update it again with the solution if I find one, thank you.

UPDATE 2

The issue was solved and the solution can be found on the new issue thread I've mentioned above.


Solution

  • You can use YourModel.readonly.find_with_deleted(params[:id]) or YourModel.readonly.with_deleted.find(params[:id])