TravisCI

Adds methods to return attribute hashes rather than instantiated ActiveRecord objects. This is useful when working with large sets of records and you need the performance more than you need the convenience of your ActiveRecord methods.

Methods

The hashes that return are similar to the hash that returns with ActiveRecord::Base#attributes, except values are not coerced (e.g., a tinyint column containing 1 will not be coerced into true). Strings, dates, and numbers will return as their appropriate Ruby object. Booleans will return the underlying representation and serialized objects will return as strings.

  • all_as_hashes works similarly to all, but returns the results as an array of hashes.
  • first_as_hash works similarly to first, but returns a hash of the first matching record.

Examples

> User.where(:email => nil).all_as_hashes
=> [
  {:id => 123, :name => "Joe User", :email => nil},
  {:id => 456, :name => "Jane User", :email => nil}
]

> User.where(:email => nil).first_as_hash
=> {:id => 123, :name => "Joe User", :email => nil}

Effects

These methods are not for everyday use, but for when speed of execution is more important than convenience, like heavy-duty reporting or processing lots of existing records.

In one example, looping through 36,400 User records and accessing an attribute used 32% less memory and performed 17% quicker using all_as_hashes over all.

Tests

Run tests with bundle exec rake test (or just rake test if you're daring).