Forked from Envek/preload_first_n_records_of_association.rb
Created
September 28, 2023 17:32
-
-
Save Mayurifag/53d50f967e50006e009606bbaffc9477 to your computer and use it in GitHub Desktop.
Preload first N records of an association in ActiveRecord (Ruby on Rails)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Collection has and belongs to many Projects through CollectionProject | |
# Usage | |
@collections = current_user.collections.page(1) | |
ActiveRecord::Associations::Preloader.new.preload(@collections, :projects, limited_projects(3)) | |
# Now @collections.first.projects will have only first 3 projects accessible | |
# Constructs SQL like this to preload limited number of recent projects along with collections: | |
# SELECT * FROM ( | |
# SELECT "projects".*, row_number() OVER (PARTITION BY collection_projects.collection_id ORDER BY collection_projects.created_at) AS collection_position | |
# FROM "projects" INNER JOIN "collection_projects" ON "collection_projects"."project_id" = "projects"."id" | |
# ) | |
# WHERE collection_position <= :limit | |
def limited_projects(limit) | |
collection_projects = CollectionProject.arel_table | |
projects = Project.arel_table | |
window = Arel::Nodes::Window.new.partition(collection_projects[:collection_id]).order(collection_projects[:created_at]) | |
row_number = Arel::Nodes::NamedFunction.new("row_number", []).over(window) | |
projects_with_collection_position = Project.all.select(projects[Arel.star], row_number).joins(:collection_projects) | |
Project.from(projects_with_collection_position, :projects).where("row_number <= :limit", {limit: limit}) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment