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
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.
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. 👍
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]
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.
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.
I am out of ideas. I just upgraded to Rails 5, is there some new magic I am not aware of?
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...
acts_as_paranoid without_default_scope: true
product_controller.rb index action
@products = Product.without_deleted.order("position")
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.
Join 29,763+ developers who get early access to new screencasts, articles, guides, updates, and more.