Simple module which can be included into ActiveRecord::Relation
- it allows to fetch data as ruby Structs instead of ActiveRecord
classes.
Fetching data this way is a bit faster and wrapping data into Structs allows to use query results like any other Ruby objects.
When running Rails: add this line to your application's Gemfile:
gem 'to_poro'
and include ToPoro
module into ActiveRecord::Relation
class.
One way of doing it is by adding a config/to_poro_initializer.rb
:
class ActiveRecord::Relation
include ToPoro
end
For example when fetching Event
data and only id
and price
is needed:
Event.all.to_poro(:id, :date)
=> [
#<struct id=1, date=Tue, 07 Jun 2016>,
#<struct id=2, date=Wed, 08 Jun 2016>,
...
]
Event.where(deleted: false).to_poro(:id, :date)
=> [
#<struct id=1, date=Tue, 07 Jun 2016>,
#<struct id=2, date=Wed, 08 Jun 2016>,
...
]
Event.where(deleted: false).limit(2).to_poro(:id, :date)
=> [
#<struct id=1, date=Tue, 07 Jun 2016>,
#<struct id=2, date=Wed, 08 Jun 2016>
]
The gem is available as open source under the terms of the MIT License.