Skip to content

toArray / to_a

rel.to_a

Convert a relation to a plain array for further processing or output.

Example: I’ve built my query and now want the results as an array for JSON serialization.

This operator materializes a relation into a plain array of tuples/objects. This is typically the final step in a query chain when you need to work with the results as regular data.

None. Works on any relation.

suppliers
.restrict(city: "Paris")
.project([:sid, :name])
.to_a
=> [{:sid=>"S2", :name=>"Jones"}]
require 'json'
data = suppliers
.project([:name, :city])
.to_a
JSON.generate(data)
# => '[{"name":"Smith","city":"London"},{"name":"Jones","city":"Paris"}]'