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?
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With