Preload Pluck

Build Status Coverage Status Dependency Status

Adds a preload_pluck method to ActiveRecord that allows querying using Rails 4 eager loading-style for joined tables (preload), and returns a 2-dimensional array without ActiveRecord model creation overhead (pluck).

The typical use case is for querying and displaying tabular data, such as on an index page, without any further manipulation needed by the involved ActiveRecord models. Data may originate from immediate attributes on the current model or from attributes from other models associated via belongs_to.

Note: Preload Pluck may not always increase query performance - always benchmark with your own queries and production data.

Install

Add to the preload_pluck gem to your Gemfile:

gem 'preload_pluck'

Usage

Call preload_pluck on an ActiveRecord model:

Comment.order(:created_at).preload_pluck(:text, 'user.name')

This will return a 2-dimensional array where columns correspond to the passed arguments:

[
  ['That was an interesting post', 'Alice']
  ['I thought so too', 'Bob']
]

Attributes on the current model can be supplied by name:

Comment.preload_pluck(:title, :text)

Nested attributes should be separated by a period:

Comment.preload_pluck('post.title', 'post.text')

Both immediate and nested attributes can be mixed:

Comment.preload_pluck(:title, :text, 'post.title', 'post.text')

Any SQL conditions (e.g. where clauses, scopes, orders, limits) should be set before preload_pluck is called:

Comment.order(:created_at)
       .joins(:user)
       .where(user: {name: 'Alice'))
       .preload_pluck(:title, :text, 'post.title', 'post.text')

See spec/preload_pluck_spec.rb for more examples.

Running Tests

SQLite must be installed before running tests.

To run tests:

bundle
rspec

By default, performance tests are disabled as it takes several minutes to insert data. To run performance tests:

rspec --tag performance

License

Copyright Assetricity, LLC

Preload Pluck is released under the MIT License. See LICENSE for details.