Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Extend ActiveRecord pluck to return array of hashes

License

NotificationsYou must be signed in to change notification settings

girishso/pluck_to_hash

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Extends ActiveRecord by addingpluck_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.

Supportspluck_to_struct since version 0.3.0.pluck_to_struct returns an array ofstructs.

Gem VersionBuild Status

Installation

Add this line to your application's Gemfile:

gem'pluck_to_hash'

And then execute:

$ bundle

Or install it yourself as:

$ gem install pluck_to_hash

Usage

Usage is similar toActiveRecord.pluck, for example

Post.limit(2).pluck_to_hash(:id,:title)## [{:id=>213, :title=>"foo"}, {:id=>214, :title=>"bar"}]#Post.limit(2).pluck_to_hash(:id)## [{:id=>213}, {:id=>214}]#

Or use the shorter aliaspluck_h

Post.limit(2).pluck_h(:id)## [{:id=>213}, {:id=>214}]#

Supportsselect alias

User.pluck_to_hash(:id,'created_at::date as my_date','created_at::time as my_time')## [{:id=>23, :my_date=>Fri, 11 Jul 2014, :my_time=>2000-01-01 07:54:36 UTC},#  {:id=>2, :my_date=>Tue, 01 Jul 2014, :my_time=>2000-01-01 14:36:15 UTC}]#

Acceptsblock parameter

User.pluck_to_hash(:id,:title)do |user_hash|  ...end

Allows specifying the type of hash. Defaults to HashWithIndifferentAccess

User.pluck_to_hash(:id,:title,hash_type:CustomHash)do |custom_hash|  ...end

Usingpluck_to_struct

posts=Post.limit(2).pluck_to_struct(:id,:title)## [#<struct id=1, title="foo">, #<struct id=2, title="bar">]#posts.first.id# 1posts.first.title# "foo"

or use the shorter aliaspluck_s

posts=Post.limit(2).pluck_s(:id,:title)## [#<struct id=1, title="foo">, #<struct id=2, title="bar">]#

Supportsblock parameter as well

Post.limit(2).pluck_to_struct(:id,:title)do |post_struct|putspost_struct.titleend

Allows specifying the type of struct. Defaults to standard Struct.

Post.limit(2).pluck_to_struct(:id,:title,struct_type:OtherStructType)do |post_struct|putspost_struct.titleend

Using with Sinatra or other non-rails frameworks without ActiveSupport

Use version0.1.4 that removes ActiveSupport dependency.HashWithIndifferentAccess is not used in that case.

Why notActiveRecord.select orActiveRecord.as_json?

Here 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 thatpluck_to_hash is almost 4x faster thanselect and about 8x faster thanas_json. As with all the "benchmarks", you should take these results with a pinch of salt!

# Node.pluck_h(:id, :title)# Node.select(:id, :title).to_a# Node.select(:id, :title).as_jsonusersystemtotalrealpluck_to_hash0.1450000.0050000.150000(0.164836)select0.5666670.0100000.576667(0.590911)as_json1.1966670.0100001.206667(1.222286)

Contributing

  1. Fork it (https://github.com/girishso/pluck_to_hash/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Licence

MIT License

Brought to you by:Cube Root Software © 2016

About

Extend ActiveRecord pluck to return array of hashes

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp