ConstantTableSaver

Loads all records from the table on first use, and thereafter returns the cached (and frozen) records for all find calls.

Optionally, creates class-level methods you can use to grab the records, named after the name field you specify.

Compatibility

Currently tested against Rails 3.2.13, on Ruby 1.8.7 and 2.0.0p0. Was also tested compatible with 2.3.14, 3.0.17, and 3.1.8.

Example

Txn.all.each {|txn| .. do something with txn.txn_type ..}

- would load each txn_type individually

Txn.all(:include => :txn_type).each {|txn| .. do something with txn.txn_type ..}

- would load the txn_types in one go after the txns query, but would still need
  a query every time you load txns

But if you use:

class TxnType
  constant_table
end

Txn.all.each {|txn| .. do something with txn.txn_type ..}

- no longer requires individual txn_type loads, just every time you start the
  server (or every request, in development mode)

TxnType.all

- also cached, but:

TxnType.all(:conditions => “name LIKE ‘%foo%’”) TxnType.find(2, :lock => true)

- all still result in traditional finds, since you gave options

You can also use:

class TxnType
  constant_table :name => :label
end

Which if you have:

TxnType.create!(:label => "Customer Purchase")
TxnType.create!(:label => "Refund")

Means you will also have methods returning those records:

TxnType.customer_purchase
TxnType.refund

Optionally, you can specify a :name_prefix and/or :name_suffix.

Copyright © 2010-2013 Will Bryant, Sekuda Ltd, released under the MIT license