How do you change how the active admin search for records

701 Views Asked by At

I'm working on a Rails 6 application and in the active-admin I want to show all the records, including the ones soft-deleted with paranoia.

I did that adding scope :with_deleted, default: true.

ActiveAdmin.register Post do
  actions :all, except: [:edit, :new]
  permit_params :body, :user_id

  scope :with_deleted, default: true
end

But when I click on the view action in the active-admin dashboard the soft-deleted record I get a

ActiveRecord::RecordNotFound in Admin::PostController#show
Couldn't find Post with 'id'=2 [WHERE "post"."deleted_at" IS NULL]

How can I change the search in the active-admin?

UPDATE: I solve this adding this code to /admin/posts.rb

  controller do
    def show
      @post = Post.find_by_id(params[:id])
    end

    def scoped_collection
      Post.with_deleted
    end
  end
1

There are 1 best solutions below

0
On

You can define method find_resource on controller scope:

controller do
  def find_resource
    scoped_collection.findy(params[:id])
  end
end

Remeber: This method need return one resource, then add .first or .last to your query.

In my context I use findy and it bring one resource.

See the Customizing resource retrieval on https://activeadmin.info/2-resource-customization.html