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?
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?
This will give you desired output
User.pluck(:email, :dob)
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)
unselect()
method for better readability, or if select()
accepted arguments prepended with -
to indicate they're not wanted. as_json
feels more to do with the format (rather than the contents) of the data. –
Mitzi This will give you desired output
User.pluck(:email, :dob)
© 2022 - 2024 — McMap. All rights reserved.