How to convert rails Active Records object to key: JSON pair name

How to convert rails Active Records Object ID: hash object name for JSON object

Users.select("id,name").all

      

In json

{1:manish,2:john,3:paulo}

      

I need the output json as above, so I can use it in my JavaScript.

Please help me with this!

+3


source to share


1 answer


You can do it:



Hash[Users.select("id,name").all.map{|u| [u.id, u.name]}].to_json

      

+6


source







All Articles