Use hash conditions instead of SQL's IN
This is what we're doing in most places.
This commit is contained in:
@@ -167,7 +167,7 @@ class Budget
|
|||||||
ids += results.where(selected: true).pluck(:id) if params[:advanced_filters].include?("selected")
|
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.undecided.pluck(:id) if params[:advanced_filters].include?("undecided")
|
||||||
ids += results.unfeasible.pluck(:id) if params[:advanced_filters].include?("unfeasible")
|
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
|
results
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -194,7 +194,7 @@ class Budget
|
|||||||
ids += Investment.where(heading_id: hid).order(confidence_score: :desc).limit(max_per_heading).pluck(:id)
|
ids += Investment.where(heading_id: hid).order(confidence_score: :desc).limit(max_per_heading).pluck(:id)
|
||||||
end
|
end
|
||||||
|
|
||||||
results.where("budget_investments.id IN (?)", ids)
|
results.where(id: ids)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.search_by_title_or_id(title_or_id)
|
def self.search_by_title_or_id(title_or_id)
|
||||||
|
|||||||
@@ -45,9 +45,7 @@ class Community < ApplicationRecord
|
|||||||
private
|
private
|
||||||
|
|
||||||
def users_who_commented
|
def users_who_commented
|
||||||
topics_ids = topics.pluck(:id)
|
User.by_comments(topics)
|
||||||
query = "comments.commentable_id IN (?)and comments.commentable_type = 'Topic'"
|
|
||||||
User.by_comments(query, topics_ids)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def users_who_topics_author
|
def users_who_topics_author
|
||||||
|
|||||||
@@ -108,8 +108,10 @@ class User < ApplicationRecord
|
|||||||
scope :active, -> { where(erased_at: nil) }
|
scope :active, -> { where(erased_at: nil) }
|
||||||
scope :erased, -> { where.not(erased_at: nil) }
|
scope :erased, -> { where.not(erased_at: nil) }
|
||||||
scope :public_for_api, -> { all }
|
scope :public_for_api, -> { all }
|
||||||
scope :by_comments, ->(query, topics_ids) { joins(:comments).where(query, topics_ids).distinct }
|
scope :by_authors, ->(author_ids) { where(id: author_ids) }
|
||||||
scope :by_authors, ->(author_ids) { where("users.id IN (?)", 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
|
scope :by_username_email_or_document_number, ->(search_string) do
|
||||||
string = "%#{search_string}%"
|
string = "%#{search_string}%"
|
||||||
where("username ILIKE ? OR email ILIKE ? OR document_number ILIKE ?", string, string, string)
|
where("username ILIKE ? OR email ILIKE ? OR document_number ILIKE ?", string, string, string)
|
||||||
|
|||||||
@@ -13,11 +13,13 @@ RSpec.describe Community, type: :model do
|
|||||||
community = proposal.community
|
community = proposal.community
|
||||||
user1 = create(:user)
|
user1 = create(:user)
|
||||||
user2 = create(:user)
|
user2 = create(:user)
|
||||||
|
user3 = create(:user)
|
||||||
|
|
||||||
topic1 = create(:topic, community: community, author: user1)
|
topic1 = create(:topic, community: community, author: user1)
|
||||||
create(:comment, commentable: topic1, author: user1)
|
create(:comment, commentable: topic1, author: user1)
|
||||||
create(:comment, commentable: topic1, author: user2)
|
create(:comment, commentable: topic1, author: user2)
|
||||||
create(:topic, community: community, author: user2)
|
create(:topic, community: community, author: user2)
|
||||||
|
create(:comment, commentable: proposal, author: user3)
|
||||||
|
|
||||||
expect(community.participants).to match_array [user1, user2, proposal.author]
|
expect(community.participants).to match_array [user1, user2, proposal.author]
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user