How can I activate ActiveRecord, also return the column name for rendering in json?

def jsontest @users = User.all.limit(10) render json: @users end 

gives

 { ... "id": 7, "name": "Sage Smith", "email": "example-6@railstutorial.org", "created_at": "2013-10-17T02:29:15.638Z", "updated_at": "2013-10-17T02:29:15.638Z", "password_digest": "$2a$10$taHk3udtWN61Il5I18akj.E90AB1TmdL1BkQBKPk/4eZ7YyizGOli", "remember_token": "118f807d0773873fb5e4cd3b5d98048aef4f6f59", "admin": false ... } 

But I would like to omit some specific fields from this API, so I use pluck

 def jsontest @users = User.all.limit(10).pluck(:id, :name, :email, :created_at) ### render json: @users end 

but pluck only returns an array of values ​​when I would like to have the attributes of each object accessible with a hash key.

 [ ... 7, "Sage Smith", "example-6@railstutorial.org", "2013-10-17T02:29:15.638Z" ... ] 

So, how can I efficiently wrest values ​​and their keys?

I understand that I can zip through @users and grab keys before plucking and re-creating the hash, but I expect that there will be some convenient method that will do exactly what I want.

+31
ruby-on-rails activerecord
Dec 27 '13 at 3:42 on
source share
6 answers

Use select instead of pluck :

 def jsontest @users = User.select('id, name, email, created_at').limit(10) render json: @users end 
+18
Dec 27 '13 at 3:49
source share

The answer to the question is good, but I have one caveat. select creates a User instance for each row of the result, but pluck does not. It doesn’t matter if you return only a few objects, but if you return large batches (50, 100, etc.), you will pay a significant decrease in performance.

I ran into this problem and I switched back to rip off:

 #in user.rb def self.pluck_to_hash(keys) pluck(*keys).map{|pa| Hash[keys.zip(pa)]} end #in elsewhere.rb User.limit(:10).pluck_to_hash(['id, name, email, created_at']) 

This is ugly, but it gets the hash you want and quickly.

I updated it to reflect Mike Campbell's comment on October 11th.

+42
Jan 17 '15 at 2:14
source share

Created a simple stone pluck_to_hash to achieve this. https://github.com/girishso/pluck_to_hash

Usage 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}] # 
+12
May 01 '15 at 13:34
source share

The fastest way to return the hash of users with selected columns is to use ActiveRecord::Base.connection.select_all .

 sql = User.select('id, name, email, created_at').limit(10).to_sql @users = ActiveRecord::Base.connection.select_all(sql) render json: @users 
+3
Jan 17 '17 at 16:30
source share

@girishso gem is great, but my project is in Rails 3. It does not work.

This article is useful to me or sets pluck_all to achieve this.

Using:

 User.limit(10).pluck_all(:id, :name, :email, :created_at) 
0
Jan 17 '17 at 16:17
source share

@andrewCone - it should be like this:

 User.limit(:10).pluck_to_hash([:id, :name, :email, :created_at]) 
-one
Jan 05 '17 at 14:28
source share



All Articles