diff --git a/app/finders/environments_finder.rb b/app/finders/environments_finder.rb
index 2f1a09fcb55c9d2d91a46ac6b0651705bbebe86f..a59f8c1efa355566c2028b8692c866403b81a93c 100644
--- a/app/finders/environments_finder.rb
+++ b/app/finders/environments_finder.rb
@@ -24,6 +24,10 @@ class EnvironmentsFinder
     environments = project.environments.available
       .where(id: environment_ids).order_by_last_deployed_at.to_a
 
+    environments.select! do |environment|
+      Ability.allowed?(current_user, :read_environment, environment)
+    end
+
     if ref && commit
       environments.select! do |environment|
         environment.includes_commit?(commit)
@@ -36,9 +40,7 @@ class EnvironmentsFinder
       end
     end
 
-    environments.select do |environment|
-      Ability.allowed?(current_user, :read_environment, environment)
-    end
+    environments
   end
 
   private