{"id":13880114,"url":"https://github.com/girishso/pluck_to_hash","last_synced_at":"2025-04-09T04:07:58.128Z","repository":{"id":31341221,"uuid":"34903905","full_name":"girishso/pluck_to_hash","owner":"girishso","description":"Extend ActiveRecord pluck to return array of hashes","archived":false,"fork":false,"pushed_at":"2018-10-03T23:33:45.000Z","size":42,"stargazers_count":306,"open_issues_count":3,"forks_count":26,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-02T03:13:47.873Z","etag":null,"topics":["activerecord","hash","pluck","rails","rails5","ruby","ruby-gem","rubygems","sinatra"],"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/girishso.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-01T13:24:38.000Z","updated_at":"2025-01-29T00:42:03.000Z","dependencies_parsed_at":"2022-07-17T08:30:33.560Z","dependency_job_id":null,"html_url":"https://github.com/girishso/pluck_to_hash","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/girishso%2Fpluck_to_hash","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/girishso%2Fpluck_to_hash/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/girishso%2Fpluck_to_hash/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/girishso%2Fpluck_to_hash/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/girishso","download_url":"https://codeload.github.com/girishso/pluck_to_hash/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247974732,"owners_count":21026742,"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","hash","pluck","rails","rails5","ruby","ruby-gem","rubygems","sinatra"],"created_at":"2024-08-06T08:02:47.765Z","updated_at":"2025-04-09T04:07:58.095Z","avatar_url":"https://github.com/girishso.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"# What is that for?\n\nExtends ActiveRecord by adding `pluck_to_hash` method that returns array of hashes instead of array of arrays. Useful when plucking multiple columns for rendering json or you need to access individual fields in your view for example.\n\nSupports `pluck_to_struct` since version 0.3.0. `pluck_to_struct` returns an array of `struct`s.\n\n[![Gem Version](https://badge.fury.io/rb/pluck_to_hash.png)](http://badge.fury.io/rb/pluck_to_hash) [![Build Status](https://travis-ci.org/girishso/pluck_to_hash.svg?branch=master)](https://travis-ci.org/girishso/pluck_to_hash)\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'pluck_to_hash'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install pluck_to_hash\n\n## Usage\n\nUsage is similar to `ActiveRecord.pluck`, for example\n\n```ruby\nPost.limit(2).pluck_to_hash(:id, :title)\n#\n# [{:id=\u003e213, :title=\u003e\"foo\"}, {:id=\u003e214, :title=\u003e\"bar\"}]\n#\n\nPost.limit(2).pluck_to_hash(:id)\n#\n# [{:id=\u003e213}, {:id=\u003e214}]\n#\n```\n\nOr use the shorter alias `pluck_h`\n\n```ruby\nPost.limit(2).pluck_h(:id)\n#\n# [{:id=\u003e213}, {:id=\u003e214}]\n#\n```\n\nSupports `select` alias\n\n```ruby\nUser.pluck_to_hash(:id, 'created_at::date as my_date', 'created_at::time as my_time')\n#\n# [{:id=\u003e23, :my_date=\u003eFri, 11 Jul 2014, :my_time=\u003e2000-01-01 07:54:36 UTC},\n#  {:id=\u003e2, :my_date=\u003eTue, 01 Jul 2014, :my_time=\u003e2000-01-01 14:36:15 UTC}]\n#\n```\n\nAccepts `block` parameter\n\n```ruby\nUser.pluck_to_hash(:id, :title) do |user_hash|\n  ...\nend\n```\n\nAllows specifying the type of hash. Defaults to HashWithIndifferentAccess\n\n```ruby\nUser.pluck_to_hash(:id, :title, hash_type: CustomHash) do |custom_hash|\n  ...\nend\n```\n\n### Using `pluck_to_struct`\n\n```ruby\nposts = Post.limit(2).pluck_to_struct(:id, :title)\n#\n# [#\u003cstruct id=1, title=\"foo\"\u003e, #\u003cstruct id=2, title=\"bar\"\u003e]\n#\n\nposts.first.id\n# 1\n\nposts.first.title\n# \"foo\"\n\n```\n\nor use the shorter alias `pluck_s`\n\n```ruby\nposts = Post.limit(2).pluck_s(:id, :title)\n#\n# [#\u003cstruct id=1, title=\"foo\"\u003e, #\u003cstruct id=2, title=\"bar\"\u003e]\n#\n```\n\nSupports `block` parameter as well\n\n```ruby\nPost.limit(2).pluck_to_struct(:id, :title) do |post_struct|\n  puts post_struct.title\nend\n```\n\nAllows specifying the type of struct. Defaults to standard Struct.\n```ruby\nPost.limit(2).pluck_to_struct(:id, :title, struct_type: OtherStructType) do |post_struct|\n  puts post_struct.title\nend\n```\n\n## Using with Sinatra or other non-rails frameworks without ActiveSupport\n\nUse version `0.1.4` that removes ActiveSupport dependency. `HashWithIndifferentAccess` is not used in that case.\n\n## Why not `ActiveRecord.select` or `ActiveRecord.as_json`?\n\nHere are results of \"benchmark\" tests performed on MacBook Air. Each method did 10 runs, rejected the 2 highest and 2 lowest times and average the remaining 6. Ran these tests on about 40,000 records. We notice that `pluck_to_hash` is almost 4x faster than `select` and about 8x faster than `as_json`. As with all the \"benchmarks\", you should take these results with a pinch of salt!\n\n```ruby\n# Node.pluck_h(:id, :title)\n# Node.select(:id, :title).to_a\n# Node.select(:id, :title).as_json\n\n                    user     system      total        real\npluck_to_hash   0.145000   0.005000   0.150000 (  0.164836)\nselect          0.566667   0.010000   0.576667 (  0.590911)\nas_json         1.196667   0.010000   1.206667 (  1.222286)\n```\n\n## Contributing\n\n1. Fork it ( https://github.com/girishso/pluck_to_hash/fork )\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create a new Pull Request\n\n## Licence\nMIT License\n\nBrought to you by: [Cube Root Software](http://www.cuberoot.in) \u0026copy; 2016\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgirishso%2Fpluck_to_hash","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgirishso%2Fpluck_to_hash","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgirishso%2Fpluck_to_hash/lists"}