Low commit activity in last 3 years
No release in over a year
ActiveRecord is great, but could be better. Here are some tweaks for it.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.0
>= 1.0.0
~> 3.0
~> 1.0
>= 1.3
>= 0.7.1
>= 10.0, <= 14.0
>= 0.21

Runtime

>= 5.2.0, < 8.0.0
 Project Readme

Active Record Tweaks

Active Record is great, but could be better. Here are some tweaks for it.

Status

GitHub Build Status

Gem Version License

Coverage Status Code Climate

The above badges are generated by https://shields.io/

Installation

gem 'active_record_tweaks'

Usage

Either include it in specific record or just ActiveRecord::Base

class SomeRecord
  include ActiveRecordTweaks::Integration::InstanceMethods
  # This module is also DEPRECATED
  # See below for details
  extend  ActiveRecordTweaks::Integration::ClassMethods

  # DEPRECATED
  include ActiveRecordTweaks
end 

# or

# In a initialzer
# DEPRECATED
ActiveRecord::Base.send(:include, ActiveRecordTweaks)

#cache_key_without_timestamp

Nothing special, just like record.cache_key
But it has no timestamp so you can use it for scoped cache key
e.g. When caching with Cookie, which you want to control the expiry time independent of record update time
Usage:

  # Just like using #cache_key
  record.cache_key_without_timestamp

#cache_key_from_attributes

Nothing special, just like record.cache_key in rails 4.1
But it does not check against columns
e.g. When you have some virtual timestamp attribute method (cached or not)
Just make sure you throw some name to it or it will raise error
Alias: #cache_key_from_attribute
Usage:

  # Just like using #cache_key
  record.cache_key_from_attributes(:happy_at, :children_max_updated_at)

.cache_key

DEPRECATED
This method does NOT consider the query like filters and and sort orders.
Thus deprecated without replacement.
Rails 5 already have #cache_key in relation class: rails/rails#20884
There is also a gem for older rails: https://github.com/customink/activerecord-collection_cache_key

Usage
There is no class level cache key for ActiveRecord at the moment (4.0.1)
Passing an array to cache_digest could lead to performance issue and the key can become too long when collection is big
(rails#12726)
This is used for getting a cache key for a ActiveRecord class for all record (I don't know how to write one for Relation, could be similar)
You can use it for class level caching (like displaying all Categories or a random list of 5 users
And the cache would only expire when there is any record created, updated, or deleted (since count and maximum of updated_at are used)

Person.count # => 1000
Person.maximum(:updated_at) # => 20131106012125528738000
Person.cache_key # => "people/all/1000-20131106012125528738000"

# When record has multiple updated columns
Person.maximum(:updated_at) # => 20131106012125528738000
Person.maximum(:updated_on) # => 20141106012125528738000
Person.cache_key(:update_at, :updated_on)     # => "people/all/1000-20141106012125528738000" (not empty but has mutiple updated timestamp columns)

# Just get cache key without timestamp
Person.maximum(:updated_on) # => some timestamp
Person.cache_key(nil)     # => "people/all/1000"

# Other examples
Product.cache_key     # => "products/all/0" (empty, has updated timestamp columns or not)
Product.cache_key     # => "products/all/1" (not empty but has no updated timestamp columns)

Usage:

RecordClass.cache_key

You can also use it with multiple records (Rails 4 Record might have updated_at and updated_on)

RecordClass.cache_key(:updated_at, :updated_on)

.cache_key_without_timestamp

DEPRECATED
Same deprecation reasons and replacement suggestion as .cache_key above

Usage Just like .cache_key(nil)
But much clearer

Person.count # => 1000
Person.maximum(:updated_at) # => 20131106012125528738000
Person.cache_key_without_timestamp # => "people/all/1000"

# Other examples
Product.cache_key_without_timestamp     # => "products/all/0" (empty, has updated timestamp columns or not)
Product.cache_key_without_timestamp     # => "products/all/1" (not empty but has no updated timestamp columns)

Usage:

RecordClass.cache_key_without_timestamp