{"id":13416449,"url":"https://github.com/khiav223577/deep_pluck","last_synced_at":"2025-05-15T16:02:11.101Z","repository":{"id":44863145,"uuid":"83786353","full_name":"khiav223577/deep_pluck","owner":"khiav223577","description":"Allow you to pluck attributes from nested associations without loading a bunch of records.","archived":false,"fork":false,"pushed_at":"2024-09-14T18:06:02.000Z","size":218,"stargazers_count":458,"open_issues_count":3,"forks_count":14,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-04T09:05:27.394Z","etag":null,"topics":["activerecord","eager-loading","orm-extension","rails","rubygems"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/khiav223577.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["khiav223577"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2017-03-03T10:23:00.000Z","updated_at":"2025-02-18T02:51:32.000Z","dependencies_parsed_at":"2024-09-15T03:29:32.863Z","dependency_job_id":"0aab44a4-2223-4c19-86b9-43089af6c067","html_url":"https://github.com/khiav223577/deep_pluck","commit_stats":{"total_commits":287,"total_committers":7,"mean_commits":41.0,"dds":0.04878048780487809,"last_synced_commit":"bee761c04b5cc5332609afca0e3fc81029d51302"},"previous_names":[],"tags_count":26,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khiav223577%2Fdeep_pluck","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khiav223577%2Fdeep_pluck/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khiav223577%2Fdeep_pluck/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khiav223577%2Fdeep_pluck/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/khiav223577","download_url":"https://codeload.github.com/khiav223577/deep_pluck/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248478285,"owners_count":21110670,"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":["activerecord","eager-loading","orm-extension","rails","rubygems"],"created_at":"2024-07-30T21:00:59.017Z","updated_at":"2025-04-11T20:39:19.098Z","avatar_url":"https://github.com/khiav223577.png","language":"Ruby","funding_links":["https://github.com/sponsors/khiav223577"],"categories":["Ruby","ORM/ODM Extensions"],"sub_categories":[],"readme":"# DeepPluck\n\n[![Gem Version](https://img.shields.io/gem/v/deep_pluck.svg?style=flat)](http://rubygems.org/gems/deep_pluck)\n[![Build Status](https://github.com/khiav223577/deep_pluck/workflows/Ruby/badge.svg)](https://github.com/khiav223577/deep_pluck/actions)\n[![RubyGems](http://img.shields.io/gem/dt/deep_pluck.svg?style=flat)](http://rubygems.org/gems/deep_pluck)\n[![Code Climate](https://codeclimate.com/github/khiav223577/deep_pluck/badges/gpa.svg)](https://codeclimate.com/github/khiav223577/deep_pluck)\n[![Test Coverage](https://codeclimate.com/github/khiav223577/deep_pluck/badges/coverage.svg)](https://codeclimate.com/github/khiav223577/deep_pluck/coverage)\n\nAllow you to pluck deeply into nested associations without loading a bunch of records.\n\n## Supports\n- Ruby 2.3 ~ 2.7, 3.0 ~ 3.2\n- Rails 3.2, 4.2, 5.0, 5.1, 5.2, 6.0, 7.0, 7.1, 7.2\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'deep_pluck'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install deep_pluck\n\n## Usage\n\n### Similar to #pluck method\n\n```rb\nUser.deep_pluck(:id, :name)\n# SELECT `users`.`id`, `users`.`name` FROM `users`\n# =\u003e\n# [\n#   {'id' =\u003e 1, 'name' =\u003e 'David'},\n#   {'id' =\u003e 2, 'name' =\u003e 'Jeremy'},\n# ]\n```\n\n### Pluck attributes from nested associations\n\n```rb\nUser.deep_pluck(:name, 'posts' =\u003e :title)\n# SELECT `users`.`id`, `users`.`name` FROM `users`\n# SELECT `posts`.`user_id`, `posts`.`title` FROM `posts` WHERE `posts`.`user_id` IN (1, 2)\n# =\u003e\n# [\n#  {\n#    'name' =\u003e 'David' ,\n#    'posts' =\u003e [\n#      {'title' =\u003e 'post1'},\n#      {'title' =\u003e 'post2'},\n#    ],\n#  },\n#  {\n#    'name' =\u003e 'Jeremy',\n#    'posts' =\u003e [\n#      {'title' =\u003e 'post3'},\n#    ],\n#  },\n# ]\n```\n\n### Pluck at models\n\n```rb\nuser = User.find_by(name: 'David')\nuser.deep_pluck(:name, :posts =\u003e :title)\n# =\u003e\n# {\n#   'name' =\u003e 'David' ,\n#   :posts =\u003e [\n#     {'title' =\u003e 'post1'},\n#     {'title' =\u003e 'post2'},\n#   ],\n# }\n```\n\n### Compare with using `#as_json`\n\nAssume the following relations:\n\n\u003e User has_many Posts.\u003cbr\u003e\n\u003e Post has_many PostComments.\u003cbr\u003e\n\u003e User has_one Contact.\u003cbr\u003e\n\nAnd the following #as_json example:\n```rb\nUser.where(:name =\u003e %w(Pearl Doggy)).includes([{:posts =\u003e :post_comments}, :contact]).as_json({\n  :root =\u003e false,\n  :only =\u003e [:name, :email],\n  :include =\u003e {\n    'posts' =\u003e {\n      :only =\u003e :name,\n      :include =\u003e {\n        'post_comments' =\u003e {\n          :only =\u003e :comment,\n        },\n      },\n    },\n    'contact' =\u003e {\n      :only =\u003e :address,\n    },\n  },\n})\n\n```\nIt works as expected, but is not very DRY, repeat writing `include`, `posts`, `post_comments` so many times.\n\nNot to mention the huge performace improvement by using #deep_pluck.\n\nYou could refactor the example with #deep_pluck:\n```rb\nUser.where(:name =\u003e %w(Pearl Doggy)).deep_pluck(\n  :name,\n  :email,\n  'posts' =\u003e [:name, 'post_comments' =\u003e :comment],\n  'contact' =\u003e :address,\n)\n```\n\n### Better Performance\n\n#deep_pluck return raw hash data without loading a bunch of records, so that faster than #as_json, or #select.\n\nThe following is the benchmark test on 3 users, 6 posts, where `users` table have 14 columns and `posts` have 6 columns. As it shows, `deep_pluck` is 4x faster than `as_json`.\n\n\n```rb\n# Repeat 500 times\n# User.includes(:posts).as_json(:only =\u003e :email, :include =\u003e {:posts =\u003e {:only =\u003e :title}})\n# User.deep_pluck(:email, {'posts' =\u003e :title})\n\n                       user     system      total        real\nas_json            1.740000   1.230000   2.970000 (  3.231465)\ndeep_pluck         0.660000   0.030000   0.690000 (  0.880018)\n```\n\nThe following is the benchmark test on 10000 users, where `users` table have 46 columns. As it shows, `deep_pluck` is 40x faster than `as_json` and 4x faster than `map`.\n```rb\n# Repeat 1 times\n# User.select('account, email').map{|s| {'account' =\u003e s.account, 'email' =\u003e s.email}}\n# User.select('account, email').as_json(:only =\u003e [:account, :email])\n# User.deep_pluck(:account, :email)\n\n                       user     system      total        real\nmap                0.210000   0.000000   0.210000 (  0.225421)\nas_json            1.980000   0.060000   2.040000 (  2.042205)\ndeep_pluck         0.040000   0.000000   0.040000 (  0.051673)\n```\n\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/khiav223577/deep_pluck. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.\n\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkhiav223577%2Fdeep_pluck","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkhiav223577%2Fdeep_pluck","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkhiav223577%2Fdeep_pluck/lists"}