{"id":18656552,"url":"https://github.com/zendesk/predictive_load","last_synced_at":"2025-10-04T04:43:28.038Z","repository":{"id":12809003,"uuid":"15483917","full_name":"zendesk/predictive_load","owner":"zendesk","description":null,"archived":false,"fork":false,"pushed_at":"2024-10-06T22:56:21.000Z","size":127,"stargazers_count":11,"open_issues_count":0,"forks_count":4,"subscribers_count":297,"default_branch":"master","last_synced_at":"2025-04-01T19:09:21.915Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zendesk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-12-27T23:39:17.000Z","updated_at":"2024-07-01T22:04:11.000Z","dependencies_parsed_at":"2024-06-25T22:09:09.456Z","dependency_job_id":null,"html_url":"https://github.com/zendesk/predictive_load","commit_stats":null,"previous_names":["eac/predictive_load"],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zendesk%2Fpredictive_load","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zendesk%2Fpredictive_load/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zendesk%2Fpredictive_load/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zendesk%2Fpredictive_load/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zendesk","download_url":"https://codeload.github.com/zendesk/predictive_load/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248449911,"owners_count":21105584,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-07T07:23:57.291Z","updated_at":"2025-10-04T04:43:22.825Z","avatar_url":"https://github.com/zendesk.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://github.com/zendesk/predictive_load/actions/workflows/actions.yml/badge.svg?branch=master)](https://github.com/zendesk/predictive_load/actions/workflows/actions.yml)\n\n\npredictive_load\n===============\n\nObserves Active Record collections and notifies when a member loads an association. This allows for:\n* automatically preloading the association in a single query for all members of that collection.\n* N+1 detection logging \n\n### Automatic preloading\n\n\n```ruby\nrequire 'predictive_load'\nrequire 'predictive_load/active_record_collection_observation'\nActiveRecord::Base.include(PredictiveLoad::ActiveRecordCollectionObservation)\n\nrequire 'predictive_load/loader'\n\nActiveRecord::Relation.collection_observer = PredictiveLoad::Loader\n\nTicket.all.each do |ticket| \n  ticket.requester.identities.each { |identity| identity.account }\nend\n```\n\nProduces:\n```sql\n  SELECT `tickets`.* FROM `tickets`\n  SELECT `requesters`.* FROM `requesters` WHERE `requesters`.`id` IN (2, 7, 12, 32, 37)\n  SELECT `identities`.* FROM `identities` WHERE `identities`.`requester_id` IN (2, 7, 12, 32, 37)\n  SELECT `accounts`.* FROM `accounts` WHERE `accounts`.`id` IN (1, 2, 3)\n```\n\n### Disabling preload\n\nSome things cannot be preloaded, use `predictive_load: false`\n\n```\nhas_many :foos, predictive_load: false\n```\n\n### Instrumentation\n\nThe library can be instrumented by providing a callback, to be invoked every time automatic preloading happens. The callback must be a callable that receives two arguments:\n* The record (instance) on which the queries that triggered automatic preloading are being performed, in the form of some association call.\n* The association object, which can be inspected to check the type and name of the association.\n\nFor example, the callback could be used to emit some metrics:\n\n```ruby\nrequire \"active_support/core_ext/string\"\n\nPredictiveLoad.callback = -\u003e (record, association) do\n  METRICS_CLIENT.increment_counter(\n    \"active_record.automatic_preloads\",\n    tags: [\n      \"model:#{record.class.name.underscore}\",\n      \"association:#{association.reflection.name}\"\n    ]\n  )\nend\n```\n\n#### Known limitations:\n\n* Calling association#size will trigger an N+1 on SELECT COUNT(*). Work around by calling #length, loading all records.\n* Calling first / last will trigger an N+1.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzendesk%2Fpredictive_load","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzendesk%2Fpredictive_load","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzendesk%2Fpredictive_load/lists"}