Use hash conditions instead of SQL's IN

This is what we're doing in most places.
This commit is contained in:
Javi Martín
2020-05-19 15:50:42 +02:00
parent f6351819fa
commit f427c757ba
4 changed files with 9 additions and 7 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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)

View File

@@ -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