From: Justin Chan Date: 2006-10-17T08:53:20+09:00 Subject: Creating enums from ActiveRecord/DB table Why doesn't the following code work? require 'rubygems' require_gem "activerecord" conf = YAML::load(File.open('database.yml')) ActiveRecord::Base.establish_connection(conf['test']) class StuffType < ActiveRecord::Base has_many :Stuff end class Stuff < ActiveRecord::Base set_table_name "stuff" belongs_to :StuffType def initialize StuffType.find_all.each do |typ| class_eval %(#{typ.name} = #{typ.id}) end end end StuffType is a DB table with two columns, name, and id. E.g. 1 = small, 2 = medium, 3 = large. I want to use it to create an enum for my Stuff class. Stuff is another DB table with, among other things, a stuff_type_id column identifying what type it is. Unfortunately when I plug this code into irb and type something like Stuff::small, expecting to get 1 returned, I get undefined method `small' for Stuff:Class. What am I doing wrong? Is there a better way to do what I want?