Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove id from select active record?

Query on console

User.select('email','dob')

returns,

[#<User:0x000000084a9b08 id: nil, email: "[email protected]">,

Why am I getting id attributes in rails 4? How to get rid of this?

like image 490
Vedprakash Singh Avatar asked Nov 27 '22 03:11

Vedprakash Singh


2 Answers

Pluck only returns the values if you want keys and values then try this:

User.select('email','dob').as_json(:except => :id) In my case the desired result was a JSON object. So, inside the as_json method you can exclude any column you desire

(additionally you can invoke object methods or access associated tables as well)

like image 176
copremesis Avatar answered Feb 07 '23 14:02

copremesis


This will give you desired output

User.pluck(:email, :dob)
like image 31
Pardeep Dhingra Avatar answered Feb 07 '23 14:02

Pardeep Dhingra