Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert ActiveRecord results into an array of hashes

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?

like image 257
Avinash Mb Avatar asked Mar 15 '13 08:03

Avinash Mb


People also ask

What does ActiveRecord base do?

ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.

What is an ActiveRecord relation object?

The Relation Class. Having queries return an ActiveRecord::Relation object allows us to chain queries together and this Relation class is at the heart of the new query syntax. Let's take a look at this class by searching through the ActiveRecord source code for a file called relation.

What is ActiveRecord?

1.1 The Active Record Pattern In Active Record, objects carry both persistent data and behavior which operates on that data. Active Record takes the opinion that ensuring data access logic as part of the object will educate users of that object on how to write to and read from the database.

What is ActiveRecord in Ruby on Rails?

What is ActiveRecord? ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code. When you need to make changes to the database, you'll write Ruby code, and then run "migrations" which makes the actual changes to the database.


2 Answers

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 
like image 110
hdorio Avatar answered Sep 19 '22 22:09

hdorio


May be?

result.map(&:attributes) 

If you need symbols keys:

result.map { |r| r.attributes.symbolize_keys } 
like image 42
Elena Unanyan Avatar answered Sep 20 '22 22:09

Elena Unanyan