# Getting Valid ENUM Values in ActiveRecord

**URL:** <https://www.ruby-forum.com/t/getting-valid-enum-values-in-activerecord/68172>\
**Category:** Rails\
**Created:** [September 10, 2006, 12:54am UTC](https://www.ruby-forum.com/t/getting-valid-enum-values-in-activerecord/68172 "2006-09-10T00:54:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [September 10, 2006, 12:54am UTC](https://www.ruby-forum.com/t/getting-valid-enum-values-in-activerecord/68172/1 "2006-09-10T00:54:53Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![guest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/8dc957/32.png) [@guest](https://www.ruby-forum.com/u/guest)\
**Post date:** [September 10, 2006, 6:48am UTC](https://www.ruby-forum.com/t/getting-valid-enum-values-in-activerecord/68172/2 "2006-09-10T06:48:16Z")

</div>

On Sep 9, 2006, at 4:54 PM, Guest wrote:

> 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’?

I think you can just use:

self.class.table\_name

Also, have you look at the Acts As Enumerated plugin. It may solve  
the problem you are working on, and even if it doesn’t the code it  
will be give you an example of how to extend Active Record.

Aaron
