Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do I allow viewing of a paranoia soft-deleted show page?

Thomas Bush asked in General

I added paranoia gem to my cms application from your soft delete tutorial. I would like users to still be able to access the show page of soft deleted items -- this is where I would add a restore link.

I assumed all I would need to do was specifically set the instance in the controller show action to include the with_deleted scope.

def show
    @product = Product.with_deleted.find(params[:id])
    ...
end

This results in the following error:
Couldn't find Product with 'id'=139 [WHERE `products`.`deleted_at` IS NULL]

I am confused, entering Product.with_deleted.find(139) in the rails console works exactly as expected so I do not understand what I am missing here.

Reply

Hey Thomas,

It sounds like you might not be skipping the default before_action :set_product for show which would trip this error because the query definitely shows it wants not-deleted records. My guess would be that before_action is being run before it gets to your show action and disabling that would let your code here run which should work great. 👍

Reply

Chris, thanks for your reply. Sorry, I forgot to mention that I removed the show action from the default before_action

Here's what that currently looks like:

before_action :set_product, only: [:update, :destroy]

Reply

Welp, that looks correct as well. Somewhere you've got a query that it's hitting that doesn't seem to be the line in your show action there. Might take some more debugging to find it, but your code looks correct right now.

Reply

Any idea how to find the query? I was coming to the same conclusion myself, so I commented out the before_action set_product entirely in an attempt to break the app. The application still loads product show pages -- which I guess proves that there's something else loading products somewhere. Problem being its not in the controller and its not in the show view, so I don't really know where it could be.

  • ensured dev:cache was not enabled
  • searched for 'product =', 'product=', etc in sublime
    • products controller and products api controller commented both instances out
  • ran rails tmp:cache:clear to ensure not somehow cached
  • restarted server numerous times
  • ran spring stop

I am out of ideas. I just upgraded to Rails 5, is there some new magic I am not aware of?

Reply

For now, my solution is to turn off the default scope, even though this is something I actually want. I than manually re-enadable it in the index action. Included below incase anyone else experiences this issue. Still very confused, but I guess this works for now...

product.rb
acts_as_paranoid without_default_scope: true

product_controller.rb index action
@products = Product.without_deleted.order("position")

Reply

Sounds like you checked pretty much everything! That's all the things I can think of (and more) so I wonder what's up with that.

One last thing you might check is this: https://github.com/ruckus/active-record-query-trace which should help you find the query based on the line number. Maybe it was something obvious we're overlooking.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,329+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.