From f6351819fa3542bc253562cf928a9656d25a9bb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Tue, 19 May 2020 14:41:12 +0200 Subject: [PATCH] Simplify SQL using DISTINCT Using `pluck("DISTINCT")` was raising a warning in Rails 5.2: DEPRECATION WARNING: Dangerous query method (method whose arguments are used as raw SQL) called with non-attribute argument(s): "DISTINCT taggings.tag_id". Non-attribute arguments will be disallowed in Rails 6.0. This method should not be called with user-provided values, such as request parameters or model attributes. Known-safe values can be passed by wrapping them in Arel.sql(). Since there was only one other use of distinct, I've decided to change both of them in the same commit, even if the second one wasn't raising a warning. --- config/initializers/acts_as_taggable_on.rb | 2 +- lib/user_segments.rb | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/config/initializers/acts_as_taggable_on.rb b/config/initializers/acts_as_taggable_on.rb index df1453908..940f411b2 100644 --- a/config/initializers/acts_as_taggable_on.rb +++ b/config/initializers/acts_as_taggable_on.rb @@ -37,7 +37,7 @@ module ActsAsTaggableOn scope :public_for_api, -> do where("(tags.kind IS NULL or tags.kind = ?) and tags.id in (?)", "category", - Tagging.public_for_api.pluck("DISTINCT taggings.tag_id")) + Tagging.public_for_api.distinct.pluck("taggings.tag_id")) end include PgSearch diff --git a/lib/user_segments.rb b/lib/user_segments.rb index ae6d345be..7927b8bfd 100644 --- a/lib/user_segments.rb +++ b/lib/user_segments.rb @@ -46,10 +46,8 @@ class UserSegments def self.not_supported_on_current_budget author_ids( User.where( - "id NOT IN (SELECT DISTINCT(voter_id) FROM votes"\ - " WHERE votable_type = ? AND votes.votable_id IN (?))", - "Budget::Investment", - current_budget_investments.pluck(:id) + "id NOT IN (?)", + Vote.select(:voter_id).where(votable: current_budget_investments).distinct ) ) end