Getting Valid ENUM Values in ActiveRecord

Guest
Sat, 09 Sep 2006 22:54:53 -0000

I know that Rails doesn''t handle database ENUMs.  I''d say that
9/10, you
can get away with just using varchar instead.  However, there ARE
instances where you need certain functionality that ENUMs provide (ie:
integrity at the datalevel).

I have a working function that will show what enum types are available
for a certain column in a table (I believe this will only work for
MySQL).  I would like to take it a step further and abstract this at the
ActiveRecord::Base level.  I have the following function, which works if
placed in the individual class level (if there is a more efficient way
to do it let me know):
====================def get_enum_values(column_name)
   self.connection.columns(MyClass.table_name).each { |column|
      return column.sql_type.gsub("enum(''",
"").gsub("'')",
"").split("'',''") if column.name ==
column_name and
column.sql_type[0...4] == "enum"
   }
   return []
end
====================
The problem is that I have to explicitly call
''MyClass.table_name''.  I
know I can use ''self.class'' to get the the name of the class
that I
need.  However, how do I programmatically tell ruby to EXECUTE ''{The
class who''s name is self.class}.table_name''?

Also, where can I place this new function in my rails app so that now
all ActiveRecord child classes will inherit it?

--
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---