Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get around the ActiveAdmin::DatabaseHitDuringLoad

I'm trying to create scopes in active admin according to what I got in db. I'm rewarded with a ActiveAdmin::DatabaseHitDuringLoad (in the CI soft that we use, locally that works because the db is already loaded)

Your file, app/admin/user.rb (line 8), caused a database error while Active Admin was loading. This is most common when your database is missing or doesn't have the latest migrations applied. To prevent this error, move the code to a place where it will only be run when a page is rendered.

What I try to do is to add one scope per city I got in db

ActiveAdmin.register User do
    City.all.each do |city|
      scope city.name, :default => true do |users|
        city.users
      end
    end
end

I understand that the error is that we call City.all before the db is loaded. Is there a way to circumvent that and easily create the scopes I need?

like image 636
astreal Avatar asked Oct 23 '14 14:10

astreal


2 Answers

Go to your routes.rb and catch the exception to avoid breaking the build.

Replace

ActiveAdmin.routes(self)

with

ActiveAdmin.routes(self) rescue ActiveAdmin::DatabaseHitDuringLoad
like image 198
Tomas Agrimbau Avatar answered Nov 12 '22 04:11

Tomas Agrimbau


Actually I found an (hacky) answer, this is the same answer that was given for another question: Active Admin scopes for each instance of a related model

Basically the trick is to update the scopes in a before_filter on the controllers index action. Which in my case is:

ActiveAdmin.register User do
  controller do
    before_filter :update_scopes, :only => :index

    def update_scopes
      resource = active_admin_config
      next if resource.scopes.any? { |scope| scope.name == city.name }
      resource.scopes << (
        ActiveAdmin::Scope.new city.name do
            city.users
        end
      )
    end
  end
end
like image 37
astreal Avatar answered Nov 12 '22 04:11

astreal