From 6421916784cc8bb73925fcaa039f4cdf12e2fa74 Mon Sep 17 00:00:00 2001 From: Espartaco Palma Date: Tue, 16 Jul 2019 22:12:13 -0700 Subject: [PATCH] Avoid the use of :pluck on not_signed abilities MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When using :pluck, ActiveRecord trigger the query immediately. The above is a behavior we probably want to avoid, because implies a query to the database regarless of the role or permission. ** Before osem_1 | 07:39:11 web.1 | Started GET "/" for 172.20.0.1 at 2019-07-1207:39:11 +0000 osem_1 | 07:39:11 web.1 | (0.3ms) SELECT "conferences"."custom_domain" FROM "conferences" WHERE ("conferences"."custom_domain" IS NOT NULL) ORDER BY start_date DESC osem_1 | 07:39:11 web.1 | Processing by ConferencesController#index as HTML osem_1 | 07:39:11 web.1 | (1.3ms) SELECT "events"."id" FROM "events" WHERE "events"."state" = $ 1 [["state", "confirmed"]] osem_1 | 07:39:11 web.1 | ↳ app/models/ability.rb:39:in `not_signed_in' osem_1 | 07:39:11 web.1 | Rendering layouts/application.html.haml osem_1 | 07:39:11 web.1 | Rendering conferences/index.html.haml within layouts/application ** After osem_1 | 07:43:18 web.1 | Started GET "/" for 172.20.0.1 at 2019-07-1207:43:18 +0000 osem_1 | 07:43:19 web.1 | (0.2ms) SELECT "conferences"."custom_domain" FROM "conferences" WHERE ("conferences"."custom_domain" IS NOT NULL) ORDER BY start_date DESC osem_1 | 07:43:19 web.1 | Processing by ConferencesController#index as HTML osem_1 | 07:43:19 web.1 | Rendering layouts/application.html.haml osem_1 | 07:43:19 web.1 | Rendering conferences/index.html.haml within layouts/application --- app/models/ability.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/ability.rb b/app/models/ability.rb index a4731816..caf8f2a4 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -36,7 +36,7 @@ class Ability end # can view Commercials of confirmed Events - can :show, Commercial, commercialable_type: 'Event', commercialable_id: Event.where(state: 'confirmed').pluck(:id) + can :show, Commercial, commercialable: Event.where(state: 'confirmed') can [:show, :create], User can [:index, :show], Survey, surveyable_type: 'Conference'