All Projects → girishso → Pluck_to_hash

girishso / Pluck_to_hash

Licence: mit
Extend ActiveRecord pluck to return array of hashes

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Pluck to hash

Pluck all
A more efficient way to get data from database. Like #pluck method but return array of hashes instead.
Stars: ✭ 83 (-69.82%)
Mutual labels:  hash, activerecord, rails, rubygems
Draftsman
Ruby gem that lets you create draft versions of your database records.
Stars: ✭ 159 (-42.18%)
Mutual labels:  rails, sinatra, ruby-gem
Unread
Handle unread records and mark them as read with Ruby on Rails
Stars: ✭ 638 (+132%)
Mutual labels:  activerecord, rails, rubygems
Deep pluck
Allow you to pluck attributes from nested associations without loading a bunch of records.
Stars: ✭ 385 (+40%)
Mutual labels:  activerecord, rails, rubygems
Rails or
Cleaner syntax for writing OR Query in Rails 5, 6. And also add #or support to Rails 3 and 4.
Stars: ✭ 86 (-68.73%)
Mutual labels:  activerecord, rails, rubygems
Mobility
Pluggable Ruby translation framework
Stars: ✭ 644 (+134.18%)
Mutual labels:  activerecord, rails, ruby-gem
Second level cache
Write Through and Read Through caching library inspired by CacheMoney and cache_fu, support ActiveRecord 4, 5 and 6.
Stars: ✭ 380 (+38.18%)
Mutual labels:  activerecord, rails, rails5
Drafting
Ruby gem for saving drafts of ActiveRecord models
Stars: ✭ 41 (-85.09%)
Mutual labels:  activerecord, rails, rubygems
Activerecord where assoc
Make ActiveRecord do conditions on your associations
Stars: ✭ 126 (-54.18%)
Mutual labels:  activerecord, rails, rails5
Active hash relation
ActiveHash Relation: Simple gem that allows you to run multiple ActiveRecord::Relation using hash. Perfect for APIs.
Stars: ✭ 115 (-58.18%)
Mutual labels:  hash, activerecord, rails
sinator
Sinatra application generator
Stars: ✭ 19 (-93.09%)
Mutual labels:  rubygems, sinatra
activerecord-setops
Union, Intersect, and Difference set operations for ActiveRecord (also, SQL's UnionAll).
Stars: ✭ 21 (-92.36%)
Mutual labels:  ruby-gem, activerecord
sinatras-skeleton
Basic Sinatra Skeleton MVC CRUD App with Sprockets, Warden, ActiveRecord and PostgresQL
Stars: ✭ 13 (-95.27%)
Mutual labels:  activerecord, sinatra
tss-rb
A Ruby implementation of Threshold Secret Sharing (Shamir) as defined in IETF Internet-Draft draft-mcgrew-tss-03.txt
Stars: ✭ 22 (-92%)
Mutual labels:  rubygems, hash
Elasticsearch Rails
Elasticsearch integrations for ActiveModel/Record and Ruby on Rails
Stars: ✭ 2,896 (+953.09%)
Mutual labels:  activerecord, rails
workflow-activerecord
ActiveRecord/Rails Integration for the Workflow library
Stars: ✭ 24 (-91.27%)
Mutual labels:  activerecord, rails5
Public activity
Easy activity tracking for models - similar to Github's Public Activity
Stars: ✭ 2,822 (+926.18%)
Mutual labels:  activerecord, rails
filtered
Filters ActiveRecord queries in a nice way
Stars: ✭ 28 (-89.82%)
Mutual labels:  ruby-gem, activerecord
idy
👓 An ID obfuscator for ActiveRecord
Stars: ✭ 15 (-94.55%)
Mutual labels:  activerecord, hash
sinatra-dev-cheatsheet
A quick-and-dirty cheat sheet for creating HTML/CSS websites, and developing using Sinatra and ActiveRecord.
Stars: ✭ 44 (-84%)
Mutual labels:  activerecord, sinatra

What is that for?

Extends 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.

Supports pluck_to_struct since version 0.3.0. pluck_to_struct returns an array of structs.

Gem Version Build 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 to ActiveRecord.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 alias pluck_h

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

Supports select 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}]
#

Accepts block 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

Using pluck_to_struct

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

posts.first.id
# 1

posts.first.title
# "foo"

or use the shorter alias pluck_s

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

Supports block parameter as well

Post.limit(2).pluck_to_struct(:id, :title) do |post_struct|
  puts post_struct.title
end

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

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

Using with Sinatra or other non-rails frameworks without ActiveSupport

Use version 0.1.4 that removes ActiveSupport dependency. HashWithIndifferentAccess is not used in that case.

Why not ActiveRecord.select or ActiveRecord.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 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!

# Node.pluck_h(:id, :title)
# Node.select(:id, :title).to_a
# Node.select(:id, :title).as_json

                    user     system      total        real
pluck_to_hash   0.145000   0.005000   0.150000 (  0.164836)
select          0.566667   0.010000   0.576667 (  0.590911)
as_json         1.196667   0.010000   1.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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].