Fix N+1s because of association preloaders not actually getting called (#28339)
This commit is contained in:
parent
3f1ec16377
commit
dcc24db793
7 changed files with 12 additions and 12 deletions
|
@ -78,9 +78,9 @@ class Announcement < ApplicationRecord
|
|||
else
|
||||
scope.select("name, custom_emoji_id, count(*) as count, exists(select 1 from announcement_reactions r where r.account_id = #{account.id} and r.announcement_id = announcement_reactions.announcement_id and r.name = announcement_reactions.name) as me")
|
||||
end
|
||||
end
|
||||
end.to_a
|
||||
|
||||
ActiveRecord::Associations::Preloader.new(records: records, associations: :custom_emoji)
|
||||
ActiveRecord::Associations::Preloader.new(records: records, associations: :custom_emoji).call
|
||||
records
|
||||
end
|
||||
|
||||
|
|
|
@ -124,7 +124,7 @@ module Account::Search
|
|||
tsquery = generate_query_for_search(terms)
|
||||
|
||||
find_by_sql([BASIC_SEARCH_SQL, { limit: limit, offset: offset, tsquery: tsquery }]).tap do |records|
|
||||
ActiveRecord::Associations::Preloader.new(records: records, associations: :account_stat)
|
||||
ActiveRecord::Associations::Preloader.new(records: records, associations: [:account_stat, { user: :role }]).call
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -133,7 +133,7 @@ module Account::Search
|
|||
sql_template = following ? ADVANCED_SEARCH_WITH_FOLLOWING : ADVANCED_SEARCH_WITHOUT_FOLLOWING
|
||||
|
||||
find_by_sql([sql_template, { id: account.id, limit: limit, offset: offset, tsquery: tsquery }]).tap do |records|
|
||||
ActiveRecord::Associations::Preloader.new(records: records, associations: :account_stat)
|
||||
ActiveRecord::Associations::Preloader.new(records: records, associations: [:account_stat, { user: :role }]).call
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -111,7 +111,7 @@ class Notification < ApplicationRecord
|
|||
|
||||
# Instead of using the usual `includes`, manually preload each type.
|
||||
# If polymorphic associations are loaded with the usual `includes`, other types of associations will be loaded more.
|
||||
ActiveRecord::Associations::Preloader.new(records: grouped_notifications, associations: associations)
|
||||
ActiveRecord::Associations::Preloader.new(records: grouped_notifications, associations: associations).call
|
||||
end
|
||||
|
||||
unique_target_statuses = notifications.filter_map(&:target_status).uniq
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue