How to convert ActiveRecord results into an array of hashes
Solution 1
as_json
You should use as_json
method which converts ActiveRecord objects to Ruby Hashes despite its name
tasks_records = TaskStoreStatus.all
tasks_records = tasks_records.as_json
# You can now add new records and return the result as json by calling `to_json`
tasks_records << TaskStoreStatus.last.as_json
tasks_records << { :task_id => 10, :store_name => "Koramanagala", :store_region => "India" }
tasks_records.to_json
serializable_hash
You can also convert any ActiveRecord objects to a Hash with serializable_hash
and you can convert any ActiveRecord results to an Array with to_a
, so for your example :
tasks_records = TaskStoreStatus.all
tasks_records.to_a.map(&:serializable_hash)
And if you want an ugly solution for Rails prior to v2.3
JSON.parse(tasks_records.to_json) # please don't do it
Solution 2
May be?
result.map(&:attributes)
If you need symbols keys:
result.map { |r| r.attributes.symbolize_keys }
Solution 3
For current ActiveRecord (4.2.4+) there is a method to_hash
on the Result
object that returns an array of hashes. You can then map over it and convert to symbolized hashes:
# Get an array of hashes representing the result (column => value):
result.to_hash
# => [{"id" => 1, "title" => "title_1", "body" => "body_1"},
{"id" => 2, "title" => "title_2", "body" => "body_2"},
...
]
result.to_hash.map(&:symbolize_keys)
# => [{:id => 1, :title => "title_1", :body => "body_1"},
{:id => 2, :title => "title_2", :body => "body_2"},
...
]
See the ActiveRecord::Result docs for more info.
Solution 4
try this one :-
data = Model_name.last
data.attributes
Avinash Mb
Updated on October 30, 2021Comments
-
Avinash Mb over 2 years
I have an ActiveRecord result of a find operation:
tasks_records = TaskStoreStatus.find( :all, :select => "task_id, store_name, store_region", :conditions => ["task_status = ? and store_id = ?", "f", store_id] )
Now I want to convert this results into an array of hashes like this:
[0] -> { :task_d => 10, :store_name=> "Koramanagala", :store_region=> "India" } [1] -> { :task_d => 10, :store_name=> "Koramanagala", :store_region=> "India" } [2] -> { :task_d => 10, :store_name=> "Koramanagala", :store_region=> "India" }
so that I will be able to iterate through the array and to add more elements to hashes and later to convert the result into
JSON
for my API response. How can I do this? -
Léo about 11 years+1 For suggesting serializable_hash - this is the first time I've ever come across an answer that mentions this. Sadly I am currently using the last JSON solution, but will now look at using serializable_hash. I just need to find out how to include the class name with each record, same as you might get if you include root in JSON.
-
hdorio about 11 years@Dom웃 If I understand correctly see this: stackoverflow.com/questions/17090891/…
-
Fredrik E over 9 years@Dom see my answer below.
-
W.M. over 7 yearsCannot be simpler and clearer than this. Thank you very much.
-
Rigo over 7 yearsAnother possible way is
tasks_records = TaskStoreStatus.all.map(&:attributes)
. -
Phil over 5 yearsA note if your version of ActiveRecord is not recognising
to_hash
method: tryto_ary
instead. Strangely this worked for me. -
alexventuraio almost 4 yearsReally great solutions over here, but my question could be What are the benefits of using either
.as_json
,&:serializable_hash
and&:attributes
? Does it has to do with ActiveRecord helpers or directly with performance? thanks in advance guys! @Dom @Rigo @Fredrik E