From: Trevor Squires Date: 2006-10-18T07:49:34+09:00 Subject: Re: Creating enums from ActiveRecord/DB table Hi, does this plugin do what you want? http://agilewebdevelopment.com/plugins/acts_as_enumerated Regards, Trevor On 16-Oct-06, at 4:53 PM, Justin Chan wrote: > 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? >