Files

ActsAsStaticRecord::SingletonMethods

Public Instance Methods

[](lookup_name) click to toggle source

Similar to acts_as_enumeration model returns the record where the acts_as_static_record :key option matches lookup If no key is specified, the primary_id column is used

class User < ActiveRecord::Base
  acts_as_static_record :key => :user_name
end

Then later we can reference the objects by the user_name

User[:blythe]
User['snowgiraffe']

The key used will be the underscore version of the name. Punctuation marks are removed. The following are equivalent:

User[:blythe_snowgiraffeawesome]
User['blythe-SNOWGIRaffe   AWESome']

user = User.first
User[user.user_name] == user
# File lib/acts_as_static_record.rb, line 297
def [](lookup_name)
  (static_record_cache[:lookup]||= begin

    static_record_cache[:primary_key].inject({}) do |lookup, (k,v)|

      lookup[v.static_record_lookup_key] = v
      lookup

    end
  end)[static_record_lookup_key(lookup_name)]

end
clear_static_record_cache() click to toggle source

Clear (and reload) the record cache

# File lib/acts_as_static_record.rb, line 402
def clear_static_record_cache
  @static_record_cache = nil
end
static_record_cache() click to toggle source

The static record cache

# File lib/acts_as_static_record.rb, line 407
def static_record_cache
  @static_record_cache||= initialize_static_record_cache
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.