{"id":16072920,"url":"https://github.com/adamcooke/records-manipulator","last_synced_at":"2026-04-27T22:31:36.712Z","repository":{"id":56891366,"uuid":"84552140","full_name":"adamcooke/records-manipulator","owner":"adamcooke","description":"Add manipulations to an Active Record scope to change the records when they are finally returned from the database","archived":false,"fork":false,"pushed_at":"2017-03-10T14:08:04.000Z","size":5,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2026-01-21T16:52:17.101Z","etag":null,"topics":["activerecord","rails","relationships","ruby"],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/adamcooke.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-10T11:14:36.000Z","updated_at":"2020-01-09T00:26:18.000Z","dependencies_parsed_at":"2022-08-21T00:20:54.866Z","dependency_job_id":null,"html_url":"https://github.com/adamcooke/records-manipulator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/adamcooke/records-manipulator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adamcooke%2Frecords-manipulator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adamcooke%2Frecords-manipulator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adamcooke%2Frecords-manipulator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adamcooke%2Frecords-manipulator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/adamcooke","download_url":"https://codeload.github.com/adamcooke/records-manipulator/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adamcooke%2Frecords-manipulator/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32358509,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-27T20:07:02.737Z","status":"ssl_error","status_checked_at":"2026-04-27T20:07:00.910Z","response_time":128,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["activerecord","rails","relationships","ruby"],"created_at":"2024-10-09T08:01:49.532Z","updated_at":"2026-04-27T22:31:36.696Z","avatar_url":"https://github.com/adamcooke.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Records Manipulator for ActiveRecord\n\nThis gem consists of a simple extention to Active Record to allow you to specify manipulations for an array of records before they are returned from the database.\n\nFor example, if we have a `Post` model and we wanted to do something to it when it was returned from our database, we might do this:\n\n```ruby\nposts = Post.where(:published =\u003e true)\nposts.each { |p| p.taint! }\n```\n\nThat's fine but we had to define this at the end of our scope once we already had loaded the records from the array. This isn't very portable and doesn't allow any further chaining after you've made your changes.\n\nWith `manipulate` you can define a procedure to be executed when the records are actually returned from the database. You may wish to do this sort of thing with a presenter but sometimes that's overkill for a simple use case.\n\n```ruby\nposts = Posts.where(:published =\u003e true).manipulate do |records|\n  records.each { |p| p.taint! }\nend\n```\n\nAfter this has run, we still haven't actually made any queries but when we do we know that the posts will have the `taint!` method executed on them.\n\nFor a slightly more complex example, you might use this to include custom (non-AR) data for one of your classes. Let's say we want to preload the biography for all the users in our app from the Twitter API. Our user model might look like this:\n\n```ruby\nclass User \u003c ActiveRecord::Base\n  def self.includes_twitter_bio\n    manipulate do |records|\n      # Get all our usernames for the records we're going to be rendering\n      usernames = records.map(\u0026:twitter_username)\n\n      # Get the bios from the twitter API (this is psudocode). It accepts an array\n      # of users and returns a hash of username to bio.\n      author_bios = TwitterAPI.get_author_bios(author_usernames)\n\n      # Add the bios to the users\n      records.each do |user|\n        user.instance_variable_set(\"@twitter_bio\", author_bios[user.twitter_username])\n      end\n    end\n  end\n\n  def twitter_bio\n    @twitter_bio ||= TwitterAPI.get_author_bio(self.twitter_username)\n  end\nend\n```\n\nOnce you've added a method to your model, you can then easily use it wherever you want to pre-load this data. Here we're using it in combination with pagination. We'll only preload data for the users that are required for the requested page.\n\n```ruby\nclass UsersController \u003c ApplicationController\n\n  def index\n    users = User.includes_twitter_bio.page(params[:page])\n  end\n\nend\n```\n\n## Installation\n\n```ruby\ngem 'records_manipulator'\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadamcooke%2Frecords-manipulator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadamcooke%2Frecords-manipulator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadamcooke%2Frecords-manipulator/lists"}