ActiveRecordTemptable provides with_temptable method to cache query results using temporary table.


Add this line to your application's Gemfile:

gem 'activerecord_temptable'

And then execute:

$ bundle

Or install it yourself as:

$ gem install activerecord_temptable


Creating temp tables to save time once querying big result set multiple times(different groupings, count etc) with a complex query.


ActiveRecordTemptable.with_temptable can be used to cache complex query's result.

For example using scheme from RailsTutorial

ActiveRecordTemptable.with_temptable(current_user.feed, [:user_id]) do |microposts|
  @most_microposts =
  @not_own_microposts = microposts.where.not(user_id: current_user).to_a # note that to_a is needed to load records


After checking out the repo, run bin/setup to install dependencies and create databases. Then, run rake spec to run the tests.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to


Bug reports and pull requests are welcome on GitHub at

Code Status

Build Status