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
Generated with the Darkfish Rdoc Generator 2.