diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index f12bcee70..fc4d31ff7 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -167,7 +167,7 @@ class Budget ids += results.where(selected: true).pluck(:id) if params[:advanced_filters].include?("selected") ids += results.undecided.pluck(:id) if params[:advanced_filters].include?("undecided") ids += results.unfeasible.pluck(:id) if params[:advanced_filters].include?("unfeasible") - results = results.where("budget_investments.id IN (?)", ids) if ids.any? + results = results.where(id: ids) if ids.any? results end @@ -194,7 +194,7 @@ class Budget ids += Investment.where(heading_id: hid).order(confidence_score: :desc).limit(max_per_heading).pluck(:id) end - results.where("budget_investments.id IN (?)", ids) + results.where(id: ids) end def self.search_by_title_or_id(title_or_id) diff --git a/app/models/community.rb b/app/models/community.rb index 3a40b682c..48a6029d3 100644 --- a/app/models/community.rb +++ b/app/models/community.rb @@ -45,9 +45,7 @@ class Community < ApplicationRecord private def users_who_commented - topics_ids = topics.pluck(:id) - query = "comments.commentable_id IN (?)and comments.commentable_type = 'Topic'" - User.by_comments(query, topics_ids) + User.by_comments(topics) end def users_who_topics_author diff --git a/app/models/user.rb b/app/models/user.rb index 4bf778d91..ac530d3a1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -108,8 +108,10 @@ class User < ApplicationRecord scope :active, -> { where(erased_at: nil) } scope :erased, -> { where.not(erased_at: nil) } scope :public_for_api, -> { all } - scope :by_comments, ->(query, topics_ids) { joins(:comments).where(query, topics_ids).distinct } - scope :by_authors, ->(author_ids) { where("users.id IN (?)", author_ids) } + scope :by_authors, ->(author_ids) { where(id: author_ids) } + scope :by_comments, ->(commentables) do + joins(:comments).where("comments.commentable": commentables).distinct + end scope :by_username_email_or_document_number, ->(search_string) do string = "%#{search_string}%" where("username ILIKE ? OR email ILIKE ? OR document_number ILIKE ?", string, string, string) diff --git a/spec/models/community_spec.rb b/spec/models/community_spec.rb index 7354bc717..072e0cb36 100644 --- a/spec/models/community_spec.rb +++ b/spec/models/community_spec.rb @@ -13,11 +13,13 @@ RSpec.describe Community, type: :model do community = proposal.community user1 = create(:user) user2 = create(:user) + user3 = create(:user) topic1 = create(:topic, community: community, author: user1) create(:comment, commentable: topic1, author: user1) create(:comment, commentable: topic1, author: user2) create(:topic, community: community, author: user2) + create(:comment, commentable: proposal, author: user3) expect(community.participants).to match_array [user1, user2, proposal.author] end