Module: CachedBelongsTo::ClassMethods

Defined in:
lib/cached_belongs_to.rb

Instance Method Summary collapse

Instance Method Details

#cached_belongs_to(*args) ⇒ Object

Creates a many to one association between two models. Works exactly as ActiveRecord’s belongs_to, except that it adds caching to it.

Usage:

class Book < ActiveRecord::Base
  cached_belongs_to :author, :caches => :name
end


17
18
19
20
21
22
23
# File 'lib/cached_belongs_to.rb', line 17

def cached_belongs_to(*args)
  caches = Array(args[1].delete(:caches))

  association = belongs_to(*args)
  create_cached_belongs_to_child_callbacks(caches, association)
  create_cached_belongs_to_parent_callbacks(caches, association)
end